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

Prerender routes json #7708

Closed
wants to merge 7 commits into from
7 changes: 6 additions & 1 deletion packages/adapter-cloudflare/README.md
Expand Up @@ -32,7 +32,12 @@ import adapter from '@sveltejs/adapter-cloudflare';

export default {
kit: {
adapter: adapter()
adapter: adapter({
// bypass worker these paths (static assets, prerendered pages, etc.)
// in order to reduce worker calls
// https://developers.cloudflare.com/pages/platform/functions/routing/#creating-a-_routesjson-file
exclude: []
})
}
};
```
Expand Down
8 changes: 6 additions & 2 deletions packages/adapter-cloudflare/index.d.ts
@@ -1,11 +1,15 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';

export default function plugin(): Adapter;

export interface RoutesJSONSpec {
version: 1;
description: string;
include: string[];
exclude: string[];
}

interface AdapterOptions {
exclude?: string[];
}

export default function plugin(options: AdapterOptions): Adapter;
15 changes: 6 additions & 9 deletions packages/adapter-cloudflare/index.js
Expand Up @@ -4,13 +4,8 @@ import { fileURLToPath } from 'url';
import * as esbuild from 'esbuild';

/** @type {import('.').default} */
export default function () {
// TODO remove for 1.0
if (arguments.length > 0) {
throw new Error(
'esbuild options can no longer be passed to adapter-cloudflare — see https://github.com/sveltejs/kit/pull/4639'
);
}
export default function (options) {
const bypass = options?.exclude || [];

return {
name: '@sveltejs/adapter-cloudflare',
Expand Down Expand Up @@ -41,7 +36,7 @@ export default function () {

writeFileSync(
`${dest}/_routes.json`,
JSON.stringify(get_routes_json(builder.config.kit.appDir, written_files))
JSON.stringify(get_routes_json(builder.config.kit.appDir, written_files, bypass))
);

writeFileSync(`${dest}/_headers`, generate_headers(builder.config.kit.appDir));
Expand Down Expand Up @@ -70,15 +65,17 @@ export default function () {
/**
* @param {string} app_dir
* @param {string[]} assets
* @param {string[]} bypass
* @returns {import('.').RoutesJSONSpec}
*/
function get_routes_json(app_dir, assets) {
function get_routes_json(app_dir, assets, bypass) {
return {
version: 1,
description: 'Generated by @sveltejs/adapter-cloudflare',
include: ['/*'],
exclude: [
`/${app_dir}/immutable/*`,
...bypass,
...assets
// We're being conservative by not excluding all assets in
// /static just yet. If there are any upstream auth rules to
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-static/test/test.js
Expand Up @@ -35,7 +35,7 @@ run('spa', (test) => {
});

test('prerenders page with prerender=true', ({ cwd }) => {
assert.ok(fs.existsSync(`${cwd}/build/about.html`));
assert.ok(fs.existsSync(`${cwd}/build/about/index.html`));
});

test('renders content in fallback page when JS runs', async ({ base, page }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/prerender/prerender.js
Expand Up @@ -148,7 +148,7 @@ export async function prerender() {
const file = path.slice(config.paths.base.length + 1) || 'index.html';

if (is_html && !file.endsWith('.html')) {
return file + (file.endsWith('/') ? 'index.html' : '.html');
return file + (file.endsWith('/') ? '' : '/') + 'index.html';
}

return file;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/preview/index.js
Expand Up @@ -101,7 +101,7 @@ export async function preview(vite, vite_config, svelte_config) {
const { pathname } = new URL(/** @type {string} */ (req.url), 'http://dummy');

let filename = normalizePath(
join(svelte_config.kit.outDir, 'output/prerendered/pages' + pathname)
join(svelte_config.kit.outDir, 'output/prerendered/pages' + pathname + '/index.html')
);
let prerendered = is_file(filename);

Expand Down