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

[Fix] Add prerendered assets to Vercel config #8332

Merged
merged 5 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/old-chicken-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-static': patch
'@sveltejs/adapter-vercel': patch
---

Add prerendered assets to Vercel config
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 1 addition & 3 deletions packages/adapter-static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ See https://kit.svelte.dev/docs/page-options#prerender for more details`
assets = pages,
fallback,
precompress
} = options ??
platform?.defaults(builder.config) ??
/** @type {import('./index').AdapterOptions} */ ({});
} = options ?? platform?.defaults ?? /** @type {import('./index').AdapterOptions} */ ({});

builder.rimraf(assets);
builder.rimraf(pages);
Expand Down
83 changes: 38 additions & 45 deletions packages/adapter-static/platforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ import fs from 'fs';
* @typedef {{
* name: string;
* test: () => boolean;
* defaults: (config: any) => import('./index').AdapterOptions; // TODO
* defaults: import('./index').AdapterOptions;
* done: (builder: import('@sveltejs/kit').Builder) => void;
* }}
* Platform */

// This function is duplicated in adapter-vercel
/** @param {import('@sveltejs/kit').Builder} builder */
function vercel_routes(builder) {
function static_vercel_config(builder) {
/** @type {any[]} */
const routes = [
{
src: `/${builder.config.kit.appDir}/immutable/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
}
];
const prerendered_redirects = [];

/** @type {Record<string, { path: string }>} */
const overrides = {};

// explicit redirects
for (const [src, redirect] of builder.prerendered.redirects) {
routes.push({
prerendered_redirects.push({
src,
headers: {
Location: redirect.location
Expand All @@ -32,51 +28,48 @@ function vercel_routes(builder) {
});
}

// prerendered pages
for (const [src, page] of builder.prerendered.pages) {
routes.push({
src,
dest: `${builder.config.kit.appDir}/prerendered/${page.file}`
});
}
for (const [path, page] of builder.prerendered.pages) {
if (path.endsWith('/') && path !== '/') {
prerendered_redirects.push(
{ src: path, dest: path.slice(0, -1) },
{ src: path.slice(0, -1), status: 308, headers: { Location: path } }
);

// implicit redirects (trailing slashes)
for (const [src] of builder.prerendered.pages) {
if (src !== '/') {
routes.push({
src: src.endsWith('/') ? src.slice(0, -1) : src + '/',
headers: {
location: src
},
status: 308
});
overrides[page.file] = { path: path.slice(1, -1) };
} else {
overrides[page.file] = { path: path.slice(1) };
}
}

routes.push({
handle: 'filesystem'
});

return routes;
return {
version: 3,
routes: [
...prerendered_redirects,
{
src: `/${builder.getAppPath()}/immutable/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
},
{
handle: 'filesystem'
}
],
overrides
};
}

/** @type {Platform[]} */
export const platforms = [
{
name: 'Vercel',
test: () => !!process.env.VERCEL,
defaults: (config) => ({
pages: `.vercel/output/static/${config.kit.appDir}/prerendered`,
assets: '.vercel/output/static'
}),
defaults: {
pages: '.vercel/output/static'
},
done: (builder) => {
fs.writeFileSync(
'.vercel/output/config.json',
JSON.stringify({
version: 3,
routes: vercel_routes(builder)
})
);
const config = static_vercel_config(builder);
fs.writeFileSync('.vercel/output/config.json', JSON.stringify(config, null, ' '));
}
}
];
110 changes: 54 additions & 56 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,7 @@ const plugin = function ({ external = [], edge, split } = {}) {
functions: `${dir}/functions`
};

/** @type {any[]} */
const prerendered_redirects = [];

/** @type {Record<string, { path: string }>} */
const overrides = {};

for (const [src, redirect] of builder.prerendered.redirects) {
prerendered_redirects.push({
src,
headers: {
Location: redirect.location
},
status: redirect.status
});
}

for (const [path, page] of builder.prerendered.pages) {
if (path.endsWith('/') && path !== '/') {
prerendered_redirects.push(
{ src: path, dest: path.slice(0, -1) },
{ src: path.slice(0, -1), status: 308, headers: { Location: path } }
);

overrides[page.file] = { path: path.slice(1, -1) };
} else {
overrides[page.file] = { path: path.slice(1) };
}
}

/** @type {any[]} */
const routes = [
...prerendered_redirects,
{
src: `/${builder.getAppPath()}/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
},
{
handle: 'filesystem'
}
];
const config = static_vercel_config(builder);

builder.log.minor('Generating serverless function...');

Expand Down Expand Up @@ -97,7 +56,7 @@ const plugin = function ({ external = [], edge, split } = {}) {
`nodejs${node_version.major}.x`
);

routes.push({ src: pattern, dest: `/${name}` });
config.routes.push({ src: pattern, dest: `/${name}` });
}

/**
Expand Down Expand Up @@ -142,7 +101,7 @@ const plugin = function ({ external = [], edge, split } = {}) {
})
);

routes.push({ src: pattern, dest: `/${name}` });
config.routes.push({ src: pattern, dest: `/${name}` });
}

const generate_function = edge ? generate_edge_function : generate_serverless_function;
Expand Down Expand Up @@ -182,18 +141,7 @@ const plugin = function ({ external = [], edge, split } = {}) {

builder.log.minor('Writing routes...');

write(
`${dir}/config.json`,
JSON.stringify(
{
version: 3,
routes,
overrides
},
null,
' '
)
);
write(`${dir}/config.json`, JSON.stringify(config, null, ' '));
}
};
};
Expand Down Expand Up @@ -225,6 +173,56 @@ function get_node_version() {
return { major, full };
}

// This function is duplicated in adapter-static
/** @param {import('@sveltejs/kit').Builder} builder */
function static_vercel_config(builder) {
/** @type {any[]} */
const prerendered_redirects = [];

/** @type {Record<string, { path: string }>} */
const overrides = {};

for (const [src, redirect] of builder.prerendered.redirects) {
prerendered_redirects.push({
src,
headers: {
Location: redirect.location
},
status: redirect.status
});
}

for (const [path, page] of builder.prerendered.pages) {
if (path.endsWith('/') && path !== '/') {
prerendered_redirects.push(
{ src: path, dest: path.slice(0, -1) },
{ src: path.slice(0, -1), status: 308, headers: { Location: path } }
);

overrides[page.file] = { path: path.slice(1, -1) };
} else {
overrides[page.file] = { path: path.slice(1) };
}
}

return {
version: 3,
routes: [
...prerendered_redirects,
{
src: `/${builder.getAppPath()}/immutable/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
},
{
handle: 'filesystem'
}
],
overrides
};
}

/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {string} entry
Expand Down