Skip to content

Commit

Permalink
Support for prerendering in the Cloudflare integration (#5993)
Browse files Browse the repository at this point in the history
* Cloudflare prerender branch

* Add prerendered routes to Cloudflare routes.json

* Adding changeset

* Prevent process proxy from running during prerender phase
  • Loading branch information
matthewp committed Jan 26, 2023
1 parent 60b32d5 commit 9855db6
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/good-avocados-repeat.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 14 additions & 2 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
},
Expand Down Expand Up @@ -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',
Expand All @@ -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 });
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -202,3 +210,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
},
};
}

function prependForwardSlash(path: string) {
return path[0] === '/' ? path : '/' + path;
}
6 changes: 4 additions & 2 deletions packages/integrations/cloudflare/src/server.advanced.ts
Original file line number Diff line number Diff line change
@@ -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';

process.env = getProcessEnvProxy();
if(!isNode) {
process.env = getProcessEnvProxy();
}

type Env = {
ASSETS: { fetch: (req: Request) => Promise<Response> };
Expand Down
6 changes: 4 additions & 2 deletions packages/integrations/cloudflare/src/server.directory.ts
Original file line number Diff line number Diff line change
@@ -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';

process.env = getProcessEnvProxy();
if(!isNode) {
process.env = getProcessEnvProxy();
}

export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
Expand Down
2 changes: 2 additions & 0 deletions packages/integrations/cloudflare/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const isNode = typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]';

export function getProcessEnvProxy() {
return new Proxy(
{},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import cloudflare from '@astrojs/cloudflare';

export default defineConfig({
adapter: cloudflare(),
output: 'server',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/astro-cloudflare-prerender",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/cloudflare": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
export const prerender = true;
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>
19 changes: 19 additions & 0 deletions packages/integrations/cloudflare/test/prerender.test.js
Original file line number Diff line number Diff line change
@@ -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/');
});
});
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9855db6

Please sign in to comment.