Skip to content

Commit

Permalink
chore: reuse pre-generated pages
Browse files Browse the repository at this point in the history
  • Loading branch information
blueMountain007 committed Sep 7, 2023
1 parent 11adf35 commit ef29fba
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
24 changes: 11 additions & 13 deletions packages/libs/lambda-at-edge/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const DEFAULT_LAMBDA_CODE_DIR = "default-lambda";
export const API_LAMBDA_CODE_DIR = "api-lambda";
export const IMAGE_LAMBDA_CODE_DIR = "image-lambda";
export const ASSETS_DIR = "assets";
export const DEFAULT_BUILD_ID = 'shared-storage';

type BuildOptions = {
args?: string[];
Expand Down Expand Up @@ -543,10 +544,10 @@ class Builder {
}> {
const pagesManifest = await this.readPagesManifest();

const buildId = await fse.readFile(
path.join(this.dotNextDir, "BUILD_ID"),
"utf-8"
);
// const buildId = await fse.readFile(
// path.join(this.dotNextDir, "BUILD_ID"),
// "utf-8"
// );
const {
logLambdaExecutionTimes = false,
domainRedirects = {},
Expand All @@ -559,7 +560,7 @@ class Builder {
// in dev mode, the max access number will always be 1
const defaultInvalidationGroupNumber = 1;
const defaultBuildManifest: OriginRequestDefaultHandlerManifest = {
buildId,
buildId: DEFAULT_BUILD_ID,
logLambdaExecutionTimes,
pages: {
ssr: {
Expand Down Expand Up @@ -729,7 +730,6 @@ class Builder {
defaultBuildManifest: OriginRequestDefaultHandlerManifest,
routesManifest: RoutesManifest
) {
const buildId = defaultBuildManifest.buildId;
const basePath = routesManifest.basePath;
const nextConfigDir = this.nextConfigDir;
const nextStaticDir = this.nextStaticDir;
Expand Down Expand Up @@ -791,7 +791,7 @@ class Builder {
const destination = path.join(
assetOutputDirectory,
withBasePath(
`static-pages/${buildId}/${(relativePageFilePath as string).replace(
`static-pages/${DEFAULT_BUILD_ID}/${(relativePageFilePath as string).replace(
/^pages\//,
""
)}`
Expand Down Expand Up @@ -835,7 +835,7 @@ class Builder {
);
const destination = path.join(
assetOutputDirectory,
withBasePath(path.join("static-pages", buildId, relativePageFilePath))
withBasePath(path.join("static-pages", DEFAULT_BUILD_ID, relativePageFilePath))
);

return copyIfExists(source, destination);
Expand All @@ -857,7 +857,7 @@ class Builder {

const destination = path.join(
assetOutputDirectory,
withBasePath(path.join("static-pages", buildId, fallback))
withBasePath(path.join("static-pages", DEFAULT_BUILD_ID, fallback))
);

return copyIfExists(source, destination);
Expand Down Expand Up @@ -1069,7 +1069,6 @@ class Builder {
) {
const basePath = routesManifest.basePath;
const normalizedBasePath = basePath ? basePath.slice(1) : "";
const buildId = defaultBuildManifest.buildId;

//create invalidation url groups dir.
const directoryPath = path.join(
Expand All @@ -1078,7 +1077,7 @@ class Builder {
normalizedBasePath,
"_next",
"data",
buildId,
DEFAULT_BUILD_ID,
INVALIDATION_DATA_DIR
);

Expand Down Expand Up @@ -1112,7 +1111,6 @@ class Builder {

const basePath = routesManifest.basePath;
const normalizedBasePath = basePath ? basePath.slice(1) : "";
const buildId = defaultBuildManifest.buildId;

const copyIfExists = async (
source: string,
Expand All @@ -1128,7 +1126,7 @@ class Builder {
ASSETS_DIR,
normalizedBasePath,
"static-pages",
buildId
DEFAULT_BUILD_ID
);

//create Permanent Static Pages dir.
Expand Down
29 changes: 15 additions & 14 deletions packages/libs/lambda-at-edge/src/default-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
sentry_flush_timeout
} from "./lib/sentry";
import { renderPageToHtml } from "./services/utils/render.util";
import { DEFAULT_BUILD_ID } from "./build";

process.env.PRERENDER = "true";
process.env.DEBUGMODE = Manifest.enableDebugMode;
Expand Down Expand Up @@ -175,7 +176,7 @@ const normaliseDataRequestUri = (
manifest: OriginRequestDefaultHandlerManifest
): string => {
let normalisedUri = uri
.replace(`/_next/data/${manifest.buildId}`, "")
.replace(`/_next/data/${DEFAULT_BUILD_ID}`, "")
.replace(".json", "");

// Normalise to "/" for index data request
Expand Down Expand Up @@ -354,16 +355,16 @@ const handleRevalidation = async ({
const canonicalUrl = decodeURI(uri)
.replace(`${basePath}`, "")
.replace(`/_next/data/`, "")
.replace(`${manifest.buildId}/`, "")
.replace(`${DEFAULT_BUILD_ID}/`, "")
.replace(".json", "")
.replace(".html", "");

const htmlKey = `${(basePath || "").replace(/^\//, "")}${
!basePath ? "" : "/"
}static-pages/${manifest.buildId}/${decodeURI(canonicalUrl)}.html`;
}static-pages/${DEFAULT_BUILD_ID}/${decodeURI(canonicalUrl)}.html`;
const jsonKey = `${(basePath || "").replace(/^\//, "")}${
!basePath ? "" : "/"
}_next/data/${manifest.buildId}/${decodeURI(canonicalUrl)}.json`;
}_next/data/${DEFAULT_BUILD_ID}/${decodeURI(canonicalUrl)}.json`;

// get heads from s3
const { domainName, region } = request.origin!.s3!;
Expand Down Expand Up @@ -753,7 +754,7 @@ const handleOriginRequest = async ({
request.uri = request.uri.replace(basePath, "");
}
} else if (isHTMLPage || hasFallback) {
s3Origin.path = `${basePath}/static-pages/${manifest.buildId}`;
s3Origin.path = `${basePath}/static-pages/${DEFAULT_BUILD_ID}`;
const pageName = uri === "/" ? "/index" : uri;
request.uri = `${pageName}.html`;
checkAndRewriteUrl(manifest, request);
Expand All @@ -765,7 +766,7 @@ const handleOriginRequest = async ({
debug(`[origin-request] is json, uri: ${request.uri}`);
if (pagePath === "pages/404.html") {
// Request static 404 page from s3
s3Origin.path = `${basePath}/static-pages/${manifest.buildId}`;
s3Origin.path = `${basePath}/static-pages/${DEFAULT_BUILD_ID}`;
request.uri = pagePath.replace("pages", "");
debug(`[origin-request] is 404, uri: ${request.uri}`);
} else if (
Expand Down Expand Up @@ -798,7 +799,7 @@ const handleOriginRequest = async ({
);

if (pagePath.endsWith(".html") && !isPreviewRequest) {
s3Origin.path = `${basePath}/static-pages/${manifest.buildId}`;
s3Origin.path = `${basePath}/static-pages/${DEFAULT_BUILD_ID}`;
request.uri = pagePath.replace("pages", "");
addS3HostHeader(request, normalisedS3DomainName);

Expand Down Expand Up @@ -924,7 +925,7 @@ const handleOriginResponse = async ({
} else {
const revalidationKey = decodeURI(uri)
.replace(`_next/data/`, "")
.replace(`${manifest.buildId}/`, "")
.replace(`${DEFAULT_BUILD_ID}/`, "")
.replace(".json", "")
.replace(".html", "");

Expand Down Expand Up @@ -986,7 +987,7 @@ const handleOriginResponse = async ({
const htmlUri = uri === "/" ? "/index.html" : `${uri}.html`;
const jsonPath = `${(basePath || "").replace(/^\//, "")}${
basePath === "" ? "" : "/"
}_next/data/${manifest.buildId}${decodeURI(htmlUri).replace(
}_next/data/${DEFAULT_BUILD_ID}${decodeURI(htmlUri).replace(
".html",
".json"
)}`;
Expand Down Expand Up @@ -1064,7 +1065,7 @@ const handleOriginResponse = async ({
Bucket: bucketName,
Key: `${(basePath || "").replace(/^\//, "")}${
basePath === "" ? "" : "/"
}static-pages/${manifest.buildId}${decodeURI(htmlUri)}`,
}static-pages/${DEFAULT_BUILD_ID}${decodeURI(htmlUri)}`,
Body: html,
ContentType: "text/html",
CacheControl: isAbTestPath(manifest, htmlUri)
Expand Down Expand Up @@ -1156,9 +1157,9 @@ const handleOriginResponse = async ({
Bucket: bucketName,
Key: `${(basePath || "").replace(/^\//, "")}${
basePath === "" ? "" : "/"
}static-pages/${manifest.buildId}/${decodeURI(normaliseUri(request.uri))
}static-pages/${DEFAULT_BUILD_ID}/${decodeURI(normaliseUri(request.uri))
.replace(`/_next/data/`, "")
.replace(`${manifest.buildId}/`, "")
.replace(`${DEFAULT_BUILD_ID}/`, "")
.replace(".json", ".html")}`,
Body: html,
ContentType: "text/html",
Expand Down Expand Up @@ -1197,7 +1198,7 @@ const handleOriginResponse = async ({
// If route has fallback, return that page from S3, otherwise return 404 page
const s3Key = `${(basePath || "").replace(/^\//, "")}${
basePath === "" ? "" : "/"
}static-pages/${manifest.buildId}${hasFallback.fallback || "/404.html"}`;
}static-pages/${DEFAULT_BUILD_ID}${hasFallback.fallback || "/404.html"}`;

debug(`[origin-response] has fallback: ${JSON.stringify(hasFallback)}`);

Expand Down Expand Up @@ -1463,7 +1464,7 @@ export const generatePermanentPageResponse = async (
//get page from S3
const s3Key = `${(basePath || "").replace(/^\//, "")}${
basePath === "" ? "" : "/"
}static-pages/${manifest.buildId}${PERMANENT_STATIC_PAGES_DIR}${uri}`;
}static-pages/${DEFAULT_BUILD_ID}${PERMANENT_STATIC_PAGES_DIR}${uri}`;

const getStream = await import("get-stream");

Expand Down
3 changes: 2 additions & 1 deletion packages/libs/lambda-at-edge/src/routing/notfound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Readable } from "stream";
import { OriginRequestDefaultHandlerManifest } from "../../types";
// @ts-ignore
import * as _ from "../lib/lodash";
import { DEFAULT_BUILD_ID } from "../build";

/**
* Return a 404 response.
Expand All @@ -25,7 +26,7 @@ export async function createNotFoundResponse(
): Promise<CloudFrontResultResponse> {
const s3Key = `${(basePath || "").replace(/^\//, "")}${
basePath === "" ? "" : "/"
}static-pages/${manifest.buildId}/404.html`;
}static-pages/${DEFAULT_BUILD_ID}/404.html`;

const getStream = await import("get-stream");

Expand Down
8 changes: 5 additions & 3 deletions packages/libs/lambda-at-edge/src/services/resource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DEFAULT_BUILD_ID } from "../build";

export interface FallbackRoute {
routeRegex: string;
fallback: string | false;
Expand Down Expand Up @@ -39,7 +41,7 @@ export class Resource {
public getHtmlKey(): string {
return `${(this.basePath || "").replace(/^\//, "")}${
!this.basePath ? "" : "/"
}static-pages/${this.buildId}${this.getCanonicalUri()}.html`;
}static-pages/${DEFAULT_BUILD_ID}${this.getCanonicalUri()}.html`;
}

public getHtmlUri(): string {
Expand All @@ -49,7 +51,7 @@ export class Resource {
public getJsonKey(): string {
return `${(this.basePath || "").replace(/^\//, "")}${
!this.basePath ? "" : "/"
}_next/data/${this.buildId}${this.getCanonicalUri()}.json`;
}_next/data/${DEFAULT_BUILD_ID}${this.getCanonicalUri()}.json`;
}

public getJsonUri(): string {
Expand All @@ -76,7 +78,7 @@ export class Resource {
public getCanonicalUri(): string {
return this.getNormalUri()
.replace(`${this.basePath}`, "")
.replace(`_next/data/${this.buildId}/`, "")
.replace(`_next/data/${DEFAULT_BUILD_ID}/`, "")
.replace(".json", "")
.replace(".html", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
RoutesManifest
} from "@getjerry/lambda-at-edge/types";
import {
deleteOldStaticAssets,
uploadStaticAssets,
uploadStaticAssetsFromBuild
} from "@getjerry/s3-static-assets";
Expand Down Expand Up @@ -364,11 +363,6 @@ class NextjsComponent extends Component {

// If new BUILD_ID file is present, remove all versioned assets but the existing build ID's assets, to save S3 storage costs.
// After deployment, only the new and previous build ID's assets are present. We still need previous build assets as it takes time to propagate the Lambda.
await deleteOldStaticAssets({
bucketName: bucketOutputs.name,
basePath: routesManifest.basePath,
credentials: this.context.credentials.aws
});

// This input is intentionally undocumented but it acts a short-term killswitch in case of any issues with uploading from the built assets.
// TODO: remove once proven stable.
Expand Down

0 comments on commit ef29fba

Please sign in to comment.