Support for prerendering in the Cloudflare integration (#5993)
* Cloudflare prerender branch * Add prerendered routes to Cloudflare routes.json * Adding changeset * Prevent process proxy from running during prerender phase
This commit is contained in:
parent
60b32d5856
commit
9855db676e
11 changed files with 93 additions and 6 deletions
7
.changeset/good-avocados-repeat.md
Normal file
7
.changeset/good-avocados-repeat.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
'@astrojs/cloudflare': patch
|
||||
---
|
||||
|
||||
Support for prerendering in the Cloudflare integration
|
||||
|
||||
This fixes prerendering in the Cloudflare adapter. Now any prerendered routes are added to the `_routes.json` config so that the worker script is skipped for those routes.
|
|
@ -49,7 +49,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
|
|||
build: {
|
||||
client: new URL(`.${config.base}`, config.outDir),
|
||||
server: new URL(`.${SERVER_BUILD_FOLDER}`, config.outDir),
|
||||
serverEntry: '_worker.js',
|
||||
serverEntry: '_worker.mjs',
|
||||
},
|
||||
});
|
||||
},
|
||||
|
@ -88,10 +88,11 @@ export default function createIntegration(args?: Options): AstroIntegration {
|
|||
vite.ssr.target = vite.ssr.target || 'webworker';
|
||||
}
|
||||
},
|
||||
'astro:build:done': async () => {
|
||||
'astro:build:done': async ({ pages }) => {
|
||||
const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)),
|
||||
entryUrl = new URL(_buildConfig.serverEntry, _config.outDir),
|
||||
buildPath = fileURLToPath(entryUrl);
|
||||
|
||||
await esbuild.build({
|
||||
target: 'es2020',
|
||||
platform: 'browser',
|
||||
|
@ -106,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 });
|
||||
|
@ -143,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())
|
||||
|
@ -202,3 +210,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
|
|||
},
|
||||
};
|
||||
}
|
||||
|
||||
function prependForwardSlash(path: string) {
|
||||
return path[0] === '/' ? path : '/' + path;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import type { SSRManifest } from 'astro';
|
||||
import { App } from 'astro/app';
|
||||
import { getProcessEnvProxy } from './util.js';
|
||||
import { getProcessEnvProxy, isNode } from './util.js';
|
||||
|
||||
if(!isNode) {
|
||||
process.env = getProcessEnvProxy();
|
||||
}
|
||||
|
||||
type Env = {
|
||||
ASSETS: { fetch: (req: Request) => Promise<Response> };
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
import type { SSRManifest } from 'astro';
|
||||
import { App } from 'astro/app';
|
||||
import { getProcessEnvProxy } from './util.js';
|
||||
import { getProcessEnvProxy, isNode } from './util.js';
|
||||
|
||||
if(!isNode) {
|
||||
process.env = getProcessEnvProxy();
|
||||
}
|
||||
|
||||
export function createExports(manifest: SSRManifest) {
|
||||
const app = new App(manifest);
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export const isNode = typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]';
|
||||
|
||||
export function getProcessEnvProxy() {
|
||||
return new Proxy(
|
||||
{},
|
||||
|
|
7
packages/integrations/cloudflare/test/fixtures/prerender/astro.config.mjs
vendored
Normal file
7
packages/integrations/cloudflare/test/fixtures/prerender/astro.config.mjs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
import cloudflare from '@astrojs/cloudflare';
|
||||
|
||||
export default defineConfig({
|
||||
adapter: cloudflare(),
|
||||
output: 'server',
|
||||
});
|
9
packages/integrations/cloudflare/test/fixtures/prerender/package.json
vendored
Normal file
9
packages/integrations/cloudflare/test/fixtures/prerender/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "@test/astro-cloudflare-prerender",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@astrojs/cloudflare": "workspace:*",
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
8
packages/integrations/cloudflare/test/fixtures/prerender/src/pages/index.astro
vendored
Normal file
8
packages/integrations/cloudflare/test/fixtures/prerender/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Testing</h1>
|
||||
</body>
|
||||
</html>
|
11
packages/integrations/cloudflare/test/fixtures/prerender/src/pages/one.astro
vendored
Normal file
11
packages/integrations/cloudflare/test/fixtures/prerender/src/pages/one.astro
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
export const prerender = true;
|
||||
---
|
||||
<html>
|
||||
<head>
|
||||
<title>Testing</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Testing</h1>
|
||||
</body>
|
||||
</html>
|
19
packages/integrations/cloudflare/test/prerender.test.js
Normal file
19
packages/integrations/cloudflare/test/prerender.test.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { loadFixture } from './test-utils.js';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('Prerendering', () => {
|
||||
/** @type {import('./test-utils').Fixture} */
|
||||
let fixture;
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
root: './fixtures/prerender/',
|
||||
});
|
||||
await fixture.build();
|
||||
});
|
||||
|
||||
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/');
|
||||
});
|
||||
});
|
|
@ -2645,6 +2645,14 @@ importers:
|
|||
'@astrojs/cloudflare': link:../../..
|
||||
astro: link:../../../../../astro
|
||||
|
||||
packages/integrations/cloudflare/test/fixtures/prerender:
|
||||
specifiers:
|
||||
'@astrojs/cloudflare': workspace:*
|
||||
astro: workspace:*
|
||||
dependencies:
|
||||
'@astrojs/cloudflare': link:../../..
|
||||
astro: link:../../../../../astro
|
||||
|
||||
packages/integrations/deno:
|
||||
specifiers:
|
||||
astro: workspace:*
|
||||
|
|
Loading…
Reference in a new issue