Skip to content

Commit

Permalink
fix: /_next/static in subdir not excluded from routing (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Jul 24, 2023
1 parent f08f2b9 commit 551fa05
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-ghosts-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Fix the `/_next/static` exclusion in `_routes.json` being incorrect when the directory is not in the root.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export async function buildApplication({
disableChunksDedup,
disableWorkerMinification,
});
await buildMetadataFiles(outputDir);

const totalBuildTime = ((Date.now() - buildStartTime) / 1000).toFixed(2);
cliLog(`Build completed in ${totalBuildTime.toLocaleString()}s`);
Expand Down Expand Up @@ -121,6 +120,8 @@ async function prepareAndBuildWorker(
!disableWorkerMinification,
);

await buildMetadataFiles(outputDir, { staticAssets });

printBuildSummary(staticAssets, processedVercelOutput, processedFunctions);
await writeBuildInfo(
join(outputDir, '_worker.js'),
Expand Down
32 changes: 28 additions & 4 deletions packages/next-on-pages/src/buildApplication/buildMetadataFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { getPhaseRoutes, getVercelConfig } from './getVercelConfig';
/**
* Builds metadata files needed for the worker to correctly run.
*/
export async function buildMetadataFiles(outputDir: string) {
export async function buildMetadataFiles(
outputDir: string,
opts: BuildMetadataFilesOpts,
) {
await Promise.all([
buildNextStaticHeaders(outputDir),
buildRoutes(outputDir),
buildRoutes(outputDir, opts),
]);
}

Expand Down Expand Up @@ -41,15 +44,17 @@ ${Object.entries(nextStaticHeaders)
}
}

async function buildRoutes(outputDir: string) {
async function buildRoutes(outputDir: string, opts: BuildMetadataFilesOpts) {
const nextStaticPath = getNextStaticDirPath(opts);

try {
await writeFile(
join(outputDir, '_routes.json'),
JSON.stringify({
version: 1,
description: `Built with @cloudflare/next-on-pages@${nextOnPagesVersion}.`,
include: ['/*'],
exclude: ['/_next/static/*'],
exclude: [`${nextStaticPath}/*`],
}),
{ flag: 'ax' }, // don't generate file if it's already manually maintained
);
Expand All @@ -59,3 +64,22 @@ async function buildRoutes(outputDir: string) {
}
}
}

/**
* Finds the path to the `/_next/static` directory from the list of static assets. Accounts for the
* path being inside sub-directories, e.g. `/blog/_next/static`, and falls back to `/_next/static`.
*
* @param opts Options for building metadata files.
* @returns The path to the `/_next/static` directory.
*/
function getNextStaticDirPath({
staticAssets,
}: BuildMetadataFilesOpts): string {
const regex = /^(.*\/_next\/static)\/.+$/;
const asset = staticAssets.find(a => regex.test(a));
return asset?.match(regex)?.[1] ?? '/_next/static';
}

type BuildMetadataFilesOpts = {
staticAssets: string[];
};

0 comments on commit 551fa05

Please sign in to comment.