-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathplatforms.js
More file actions
107 lines (95 loc) · 2.76 KB
/
Copy pathplatforms.js
File metadata and controls
107 lines (95 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import fs from 'node:fs';
import process from 'node:process';
/**
* @typedef {{
* name: string;
* test: () => boolean;
* defaults: import('./index.js').AdapterOptions;
* done: (builder: import('@sveltejs/kit').Builder) => void;
* }}
* Platform */
// This function is duplicated in adapter-vercel
/** @param {import('@sveltejs/kit').Builder} builder */
function static_vercel_config(builder) {
/** @type {any[]} */
const prerendered_redirects = [];
/** @type {Record<string, { path: string }>} */
const overrides = {};
for (let [src, redirect] of builder.prerendered.redirects) {
if (src.replace(/\/$/, '') === redirect.location.replace(/\/$/, '')) {
// ignore the extreme edge case of a `/foo` -> `/foo/` redirect,
// which would only arise if the response was generated by a
// `handle` hook or outside the app altogether (since you
// can't declaratively create both routes)
} else {
// redirect both `/foo` and `/foo/` to `redirect.location`
src = src.replace(/\/?$/, '/?');
}
prerendered_redirects.push({
src,
headers: {
Location: redirect.location
},
status: redirect.status
});
}
for (const [path, page] of builder.prerendered.pages) {
let overrides_path = path.slice(1);
if (path !== '/') {
/** @type {string | undefined} */
let counterpart_route = path + '/';
if (path.endsWith('/')) {
counterpart_route = path.slice(0, -1);
overrides_path = path.slice(1, -1);
}
prerendered_redirects.push(
{ src: path, dest: counterpart_route },
{ src: counterpart_route, status: 308, headers: { Location: path } }
);
}
overrides[page.file] = { path: overrides_path };
}
return {
version: 3,
routes: [
...prerendered_redirects,
{
src: `/${builder.getAppPath()}/immutable/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
},
{
handle: 'filesystem'
},
// Prevent incorrect caching: if a request to /_app/immutable/* doesn't match
// a static file, return 404 instead of falling through to dynamic routes.
// Otherwise, we could accidentally immutably cache dynamic content served
// by the fallback function. `no-store` stops the earlier immutable header
// from sticking to this 404, so a missing asset isn't cached for a year.
{
src: `/${builder.getAppPath()}/immutable/.+`,
status: 404,
headers: {
'cache-control': 'no-store'
},
continue: false
}
],
overrides
};
}
/** @type {Platform[]} */
export const platforms = [
{
name: 'Vercel',
test: () => !!process.env.VERCEL,
defaults: {
pages: '.vercel/output/static'
},
done: (builder) => {
const config = static_vercel_config(builder);
fs.writeFileSync('.vercel/output/config.json', JSON.stringify(config, null, ' '));
}
}
];