From fb06f48810d62c256d3a4e8ab688a0aafa55f620 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 26 Jan 2023 12:01:58 -0500 Subject: [PATCH] Add prerendered routes to Cloudflare routes.json --- packages/integrations/cloudflare/src/index.ts | 15 ++++++++--- .../cloudflare/test/prerender.test.js | 25 +++---------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index e0b2d77bf..3a077c4d6 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -88,13 +88,11 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr.target = vite.ssr.target || 'webworker'; } }, - 'astro:build:done': async () => { - console.log('BUILD DONE'); + 'astro:build:done': async ({ pages }) => { const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)), entryUrl = new URL(_buildConfig.serverEntry, _config.outDir), buildPath = fileURLToPath(entryUrl); - console.log("ENTRY", buildPath); await esbuild.build({ target: 'es2020', platform: 'browser', @@ -109,6 +107,9 @@ export default function createIntegration(args?: Options): AstroIntegration { }, }); + // Rename to worker.js + await fs.promises.rename(buildPath, buildPath.replace(/\.mjs$/, '.js')); + // throw the server folder in the bin const serverUrl = new URL(_buildConfig.server); await fs.promises.rm(serverUrl, { recursive: true, force: true }); @@ -146,6 +147,10 @@ export default function createIntegration(args?: Options): AstroIntegration { .filter((file: string) => cloudflareSpecialFiles.indexOf(file) < 0) .map((file: string) => `/${file}`); + for(let page of pages) { + staticPathList.push(prependForwardSlash(page.pathname)); + } + const redirectsExists = await fs.promises .stat(new URL('./_redirects', _config.outDir)) .then((stat) => stat.isFile()) @@ -205,3 +210,7 @@ export default function createIntegration(args?: Options): AstroIntegration { }, }; } + +function prependForwardSlash(path: string) { + return path[0] === '/' ? path : '/' + path; +} diff --git a/packages/integrations/cloudflare/test/prerender.test.js b/packages/integrations/cloudflare/test/prerender.test.js index 87091c31c..a3ce50d08 100644 --- a/packages/integrations/cloudflare/test/prerender.test.js +++ b/packages/integrations/cloudflare/test/prerender.test.js @@ -1,18 +1,6 @@ import { loadFixture } from './test-utils.js'; import { expect } from 'chai'; -process.on('exit', () => { - console.log('process [exit]'); -}); - -process.on('uncaughtException', err => { - console.log('uncaughtException'); -}); - -process.on('unhandledRejection', rej => { - console.log('unhandledRejection'); -}); - describe('Prerendering', () => { /** @type {import('./test-utils').Fixture} */ let fixture; @@ -21,16 +9,11 @@ describe('Prerendering', () => { fixture = await loadFixture({ root: './fixtures/prerender/', }); - try { - console.log("BEFORE"); - await fixture.build(); - console.log('done'); - } catch(err) { - console.log('after build', err); - } + await fixture.build(); }); - it('works', async () => { - console.log('works'); + it('includes prerendered routes in the routes.json config', async () => { + const routes = JSON.parse(await fixture.readFile('/_routes.json')); + expect(routes.exclude).to.include('/one/'); }); });