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

feat: use config.kit.paths.base for static assets #4448

Merged
merged 13 commits into from Oct 12, 2022
Merged
9 changes: 9 additions & 0 deletions .changeset/smooth-waves-accept.md
@@ -0,0 +1,9 @@
---
'@sveltejs/adapter-cloudflare': patch
'@sveltejs/adapter-cloudflare-workers': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-vercel': patch
'@sveltejs/kit': patch
---

Use config.kit.paths.base prefix for static assets
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare-workers/files/entry.js
Expand Up @@ -6,7 +6,7 @@ const static_asset_manifest = JSON.parse(static_asset_manifest_json);

const server = new Server(manifest);

const prefix = `/${manifest.appDir}/`;
const app_path = `/${manifest.appPath}/`;

export default {
/**
Expand All @@ -20,12 +20,12 @@ export default {
const url = new URL(req.url);

// static assets
if (url.pathname.startsWith(prefix)) {
if (url.pathname.startsWith(app_path)) {
/** @type {Response} */
const res = await get_asset_from_kv(req, env, context);
if (is_error(res.status)) return res;

const cache_control = url.pathname.startsWith(prefix + 'immutable/')
const cache_control = url.pathname.startsWith(appPath + 'immutable/')
benmccann marked this conversation as resolved.
Show resolved Hide resolved
? 'public, immutable, max-age=31536000'
: 'no-cache';

Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare-workers/index.js
Expand Up @@ -73,8 +73,9 @@ export default function () {
});

builder.log.minor('Copying assets...');
builder.writeClient(site.bucket);
builder.writePrerendered(site.bucket);
const bucket_dir = `${site.bucket}${builder.config.kit.paths.base}`;
builder.writeClient(bucket_dir);
builder.writePrerendered(bucket_dir);
}
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare/index.js
Expand Up @@ -23,8 +23,9 @@ export default function () {
builder.rimraf(tmp);
builder.mkdirp(tmp);

const written_files = builder.writeClient(dest);
builder.writePrerendered(dest);
const dest_dir = `${dest}${builder.config.kit.paths.base}`;
const written_files = builder.writeClient(dest_dir);
builder.writePrerendered(dest_dir);

const relativePath = posix.relative(tmp, builder.getServerDirectory());

Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-cloudflare/src/worker.js
Expand Up @@ -4,7 +4,7 @@ import * as Cache from 'worktop/cfw.cache';

const server = new Server(manifest);

const prefix = `/${manifest.appDir}/`;
const app_path = `/${manifest.appPath}/`;

/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
Expand All @@ -18,11 +18,11 @@ const worker = {
let { pathname } = new URL(req.url);

// static assets
if (pathname.startsWith(prefix)) {
if (pathname.startsWith(app_path)) {
res = await env.ASSETS.fetch(req);
if (!res.ok) return res;

const cache_control = pathname.startsWith(prefix + 'immutable/')
const cache_control = pathname.startsWith(app_path + 'immutable/')
? 'public, immutable, max-age=31536000'
: 'no-cache';

Expand Down
7 changes: 4 additions & 3 deletions packages/adapter-netlify/index.js
Expand Up @@ -54,15 +54,16 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
builder.log.minor(`Publishing to "${publish}"`);

builder.log.minor('Copying assets...');
builder.writeClient(publish);
builder.writePrerendered(publish);
const publish_dir = `${publish}${builder.config.kit.paths.base}`;
builder.writeClient(publish_dir);
builder.writePrerendered(publish_dir);

builder.log.minor('Writing custom headers...');
const headers_file = join(publish, '_headers');
builder.copy('_headers', headers_file);
appendFileSync(
headers_file,
`\n\n/${builder.config.kit.appDir}/immutable/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
`\n\n/${builder.getAppPath()}/immutable/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
);

if (edge) {
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-vercel/index.js
Expand Up @@ -98,7 +98,7 @@ export default function ({ external = [], edge, split } = {}) {
const files = fileURLToPath(new URL('./files', import.meta.url).href);

const dirs = {
static: `${dir}/static`,
static: `${dir}/static${builder.config.kit.paths.base}`,
functions: `${dir}/functions`
};

Expand All @@ -118,7 +118,7 @@ export default function ({ external = [], edge, split } = {}) {
...redirects[builder.config.kit.trailingSlash],
...prerendered_redirects,
{
src: `/${builder.config.kit.appDir}/.+`,
src: `/${builder.getAppPath()}/.+`,
headers: {
'cache-control': 'public, immutable, max-age=31536000'
}
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/src/core/adapt/builder.js
Expand Up @@ -128,6 +128,10 @@ export function create_builder({ config, build_data, routes, prerendered, log })
return config.kit.files.assets;
},

getAppPath() {
return build_data.app_path;
},

writeClient(dest) {
return [...copy(`${config.kit.outDir}/output/client`, dest)];
},
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/generate_manifest/index.js
Expand Up @@ -45,6 +45,7 @@ export function generate_manifest({ build_data, relative_path, routes, format =
/** @type {import('types').SSRManifest} */
return `{
appDir: ${s(build_data.app_dir)},
appPath: ${s(build_data.app_path)},
assets: new Set(${s(assets)}),
mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
_: {
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/exports/vite/dev/index.js
Expand Up @@ -59,6 +59,7 @@ export async function dev(vite, vite_config, svelte_config) {

manifest = {
appDir: svelte_config.kit.appDir,
appPath: svelte_config.kit.appDir,
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
mimeTypes: get_mime_lookup(manifest_data),
_: {
Expand Down
4 changes: 4 additions & 0 deletions packages/kit/src/exports/vite/index.js
Expand Up @@ -379,6 +379,7 @@ function kit() {
);

log.info('Building server');

const options = {
cwd,
config: svelte_config,
Expand All @@ -395,6 +396,9 @@ function kit() {
/** @type {import('types').BuildData} */
build_data = {
app_dir: svelte_config.kit.appDir,
app_path: `${svelte_config.kit.paths.base.slice(1)}${
svelte_config.kit.paths.base ? '/' : ''
}${svelte_config.kit.appDir}`,
manifest_data,
service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
client,
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/types/index.d.ts
Expand Up @@ -85,6 +85,8 @@ export interface Builder {
getClientDirectory(): string;
getServerDirectory(): string;
getStaticDirectory(): string;
/** The application path including any configured base path */
getAppPath(): string;

/**
* @param dest the destination folder to which files should be copied
Expand Down Expand Up @@ -379,6 +381,7 @@ export interface ServerInitOptions {

export interface SSRManifest {
appDir: string;
appPath: string;
assets: Set<string>;
mimeTypes: Record<string, string>;

Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/internal.d.ts
Expand Up @@ -47,6 +47,7 @@ export interface Asset {

export interface BuildData {
app_dir: string;
app_path: string;
manifest_data: ManifestData;
service_worker: string | null;
client: {
Expand Down