Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 71b61e7

Browse files
authored
fix(lambda-at-edge): non-dynamic pages (SSG, SSR) should be prioritized over dynamic fallback pages (#685)
1 parent ed02424 commit 71b61e7

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

packages/e2e-tests/next-app-dynamic-routes/cypress/integration/pages.test.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ describe("Pages Tests", () => {
33
cy.ensureAllRoutesNotErrored();
44
});
55

6-
describe("Dynamic Pages with getStaticPaths() fallback: true", () => {
6+
describe("Dynamic Page at root level with getStaticPaths() fallback: true", () => {
77
[{ path: "/a" }, { path: "/b" }, { path: "/not-prerendered" }].forEach(
88
({ path }) => {
99
it(`serves and caches page ${path}`, () => {
@@ -36,9 +36,29 @@ describe("Pages Tests", () => {
3636
});
3737
}
3838
);
39+
40+
it("serves non-dynamic SSG page at same level", () => {
41+
const path = "/ssg-page";
42+
43+
cy.request({
44+
url: path
45+
}).then((response) => {
46+
expect(response.body).to.contain("SSG Page");
47+
});
48+
});
49+
50+
it("serves non-dynamic SSR page at same level", () => {
51+
const path = "/ssr-page";
52+
53+
cy.request({
54+
url: path
55+
}).then((response) => {
56+
expect(response.body).to.contain("SSR Page");
57+
});
58+
});
3959
});
4060

41-
describe("Nested Dynamic Pages with getStaticPaths() fallback: false", () => {
61+
describe("Nested Dynamic Page with getStaticPaths() fallback: false", () => {
4262
[{ path: "/nested/a" }, { path: "/nested/b" }].forEach(({ path }) => {
4363
it(`serves and caches page ${path}`, () => {
4464
cy.visit(path);
@@ -86,9 +106,6 @@ describe("Pages Tests", () => {
86106
failOnStatusCode: false
87107
}).then((response) => {
88108
expect(response.status).to.equal(404);
89-
expect(response.headers["x-cache"]).to.equal(
90-
"Error from cloudfront"
91-
);
92109
});
93110
});
94111
});

packages/e2e-tests/next-app-dynamic-routes/pages/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function IndexPage(props: IndexPageProps): JSX.Element {
1111
<div>
1212
{`Hello ${props.name}. This is an SSR page using getServerSideProps(). It also has an image.`}
1313
</div>
14-
<img src={"/basepath/app-store-badge.png"} alt={"An image"} />
14+
<img src={"/app-store-badge.png"} alt={"An image"} />
1515
</React.Fragment>
1616
);
1717
}

packages/libs/lambda-at-edge/src/default-handler.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ const handleOriginRequest = async ({
293293
const s3Origin = origin.s3 as CloudFrontS3Origin;
294294
const isHTMLPage = isStaticPage || isPrerenderedPage;
295295
const normalisedS3DomainName = normaliseS3OriginDomain(s3Origin);
296-
const hasFallback = hasFallbackForUri(uri, prerenderManifest);
296+
const hasFallback = hasFallbackForUri(uri, prerenderManifest, manifest);
297297
const { now, log } = perfLogger(manifest.logLambdaExecutionTimes);
298298
const previewCookies = getPreviewCookies(request);
299299
const isPreviewRequest =
@@ -496,7 +496,7 @@ const handleOriginResponse = async ({
496496
res.end(JSON.stringify(renderOpts.pageData));
497497
return await responsePromise;
498498
} else {
499-
const hasFallback = hasFallbackForUri(uri, prerenderManifest);
499+
const hasFallback = hasFallbackForUri(uri, prerenderManifest, manifest);
500500
if (!hasFallback) return response;
501501

502502
// If route has fallback, return that page from S3, otherwise return 404 page
@@ -550,8 +550,17 @@ const isOriginResponse = (
550550

551551
const hasFallbackForUri = (
552552
uri: string,
553-
prerenderManifest: PrerenderManifestType
553+
prerenderManifest: PrerenderManifestType,
554+
manifest: OriginRequestDefaultHandlerManifest
554555
) => {
556+
const {
557+
pages: { ssr, html }
558+
} = manifest;
559+
// Non dynamic routes are prioritized over dynamic fallbacks, return false to ensure those get rendered instead
560+
if (ssr.nonDynamic[uri] || html.nonDynamic[uri]) {
561+
return false;
562+
}
563+
555564
return Object.values(prerenderManifest.dynamicRoutes).find((routeConfig) => {
556565
const re = new RegExp(routeConfig.routeRegex);
557566
return re.test(uri);

0 commit comments

Comments
 (0)