Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for prerendering in the Cloudflare integration #5993

Merged
merged 4 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.