File size: 1,676 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from 'node:fs';

/**
 * @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 (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
	};
}

/** @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, '  '));
		}
	}
];