Skip to content

Commit

Permalink
fix(core, lambda-at-edge): properly encoded URIs for ISR (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
dphang authored Nov 14, 2021
1 parent e4d66d6 commit d345aca
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe("ISR Tests", () => {
describe("SSG page", () => {
[
{ path: "/revalidated-ssg-page", initialWaitSeconds: 0 },
// Page with spaces in it (generally not recommended since this is not URL-safe character, but Next.js handles this)
{ path: "/revalidated-ssg-pages-2/with%20space", initialWaitSeconds: 0 },
// Pre-rendered ISR page
{ path: "/revalidated-ssg-pages/101", initialWaitSeconds: 0 },
// Blocking dynamic generated page. As the page will be created and cached
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react";
import { GetStaticPaths, GetStaticPropsResult } from "next";

type SSGPageProps = {
date: string;
};

export default function RevalidatedSSGPage(props: SSGPageProps): JSX.Element {
return (
<React.Fragment>
<div>
<p data-cy="date-text">{props.date}</p>
</div>
</React.Fragment>
);
}

export function getStaticPaths() {
const paths = [{ params: { title: "with space" } }];
return { paths, fallback: true };
}

export function getStaticProps(): GetStaticPropsResult<SSGPageProps> {
return {
revalidate: 10,
props: {
date: new Date().toJSON()
}
};
}
4 changes: 3 additions & 1 deletion packages/libs/core/src/defaultHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ const staticRequest = async (
platformClient: PlatformClient
) => {
const basePath = routesManifest.basePath;
const fileKey = (path + file).slice(1); // need to remove leading slash from path for page/file key
const fileKey = (path + decodeURI(file)).slice(1); // need to remove leading slash from path for page/file key
// also decode file parameter as it's encoded
// (legacy reasons since previously Cloudfront request is used to request S3, and CF requires an encoded request.uri)

const staticRoute = route.isStatic ? (route as StaticRoute) : undefined;
const statusCode = route?.statusCode ?? 200;
Expand Down
28 changes: 28 additions & 0 deletions packages/libs/core/src/route/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ export const handlePageReq = (
statusCode
};
}
// Handle ISR pages with encoded URL
// This only applies to ISR, other pages should not be resolved if sent with encoded characters (same as local Next.js server behavior)
const decodedLocaleUri = decodeURI(localeUri);
if (pages.ssg.nonDynamic[decodedLocaleUri] && !isPreview) {
const ssg = pages.ssg.nonDynamic[decodedLocaleUri];
if (ssg.initialRevalidateSeconds) {
const route = ssg.srcRoute ?? decodedLocaleUri;
const nonLocaleUri = dropLocaleFromPath(
decodedLocaleUri,
routesManifest
);
const statusCode =
nonLocaleUri === "/404"
? 404
: nonLocaleUri === "/500"
? 500
: undefined;
return {
isData: false,
isStatic: true,
file: pageHtml(localeUri), // Use encoded localeUri instead of decodedLocaleUri as this is used by CloudFront request that needs an encoded URL
// page JS path is from SSR entries in manifest
page: pages.ssr.nonDynamic[route] || pages.ssr.dynamic[route],
revalidate: ssg.initialRevalidateSeconds,
statusCode
};
}
}
if ((pages.ssg.notFound ?? {})[localeUri] && !isPreview) {
return notFoundPage(uri, manifest, routesManifest);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/libs/lambda-at-edge/src/regeneration-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const handler = async (event: AWSLambda.SQSEvent): Promise<void> => {
"passthrough"
);

const normalizedUri = regenerationEvent.pageS3Path
const normalizedUri = decodeURI(regenerationEvent.pageS3Path)
.replace(`static-pages/${manifest.buildId}`, "")
.replace(".js", "");

Expand Down

0 comments on commit d345aca

Please sign in to comment.