Skip to content

Commit c18a89d

Browse files
authored
fix: Support turbo hash in preview deploy URLs (#251)
1 parent 783e917 commit c18a89d

File tree

4 files changed

+62
-101
lines changed

4 files changed

+62
-101
lines changed

terraform-module/edge-lambdas/dist/viewer-request/index.js

+46-74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform-module/edge-lambdas/dist/viewer-response/index.js

+9-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform-module/edge-lambdas/src/viewer-request/viewer-request.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ const DEFAULT_BRANCH_DEFAULT_NAME = 'master'
2020
* based on the host name specified by the request, and sets that file as the URI of the request.
2121
* The options are:
2222
* - HTML for latest app version on the main branch
23-
* e.g. app.staging.example.com and app.example.com
23+
* e.g. app.dev.example.com and app.example.com
2424
* - HTML for latest app version on a feature branch (branch preview deployment)
25-
* e.g. my-branch.staging.example.com
25+
* e.g. my-branch.dev.example.com
2626
* - HTML for a specific app version (hash preview deployment)
27-
* e.g. preview-b104213fc39ecca4f237a7bd6544d428ad46ec7e.app.staging.example.com
27+
* e.g. preview-b104213fc39ecca4f237a7bd6544d428ad46ec7e.app.dev.example.com
2828
*
2929
* If the translations are enabled via configuration, this lambda will also fetch the current translation
3030
* version from S3 and pass it to the response lambda via custom headers on the request object.
@@ -34,9 +34,6 @@ export function getHandler(config: Config, s3: S3Client) {
3434
const request = event.Records[0].cf.request
3535

3636
try {
37-
// Get app version and translation version in parallel to avoid the double network penalty.
38-
// Translation hash is only fetched if translations are enabled. Fetching translation cursor
39-
// can never throw here, as in case of a failure we're returning a default value.
4037
const appVersion = await getAppVersion(request, config, s3)
4138

4239
// Set app version header on request, so it can be picked up by the viewer response lambda
@@ -83,7 +80,7 @@ function getUri(request: CloudFrontRequest, appVersion: string) {
8380
async function getAppVersion(request: CloudFrontRequest, config: Config, s3: S3Client) {
8481
const host = getHeader(request, 'host') ?? null
8582

86-
// Preview name is the first segment of the url e.g. my-branch for my-branch.app.staging.example.com
83+
// Preview name is the first segment of the url e.g. my-branch for my-branch.app.dev.example.com
8784
// Preview name is either a sanitized branch name or it follows the preview-[hash] pattern
8885
let previewName: string
8986

@@ -105,11 +102,11 @@ async function getAppVersion(request: CloudFrontRequest, config: Config, s3: S3C
105102
}
106103

107104
/**
108-
* We serve a preview for each app version at e.g.preview-[hash].app.staging.example.com
105+
* We serve a preview for each app version at e.g.preview-[hash].app.dev.example.com
109106
* If the preview name matches that pattern, we assume it's a hash preview link
110107
*/
111108
function getPreviewHash(previewName?: string) {
112-
const matchHash = /^preview-(?<hash>[a-z0-9]{40})$/.exec(previewName || '')
109+
const matchHash = /^preview-(?<hash>[a-z0-9]{16}|[a-z0-9]{40})$/.exec(previewName || '')
113110
return matchHash?.groups?.hash
114111
}
115112

terraform-module/edge-lambdas/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es6",
3+
"target": "ES2022",
44
"moduleResolution": "node",
55
"allowSyntheticDefaultImports": true,
66
"esModuleInterop": true

0 commit comments

Comments
 (0)