Skip to content

Commit

Permalink
feat: use config.kit.paths.base for static assets
Browse files Browse the repository at this point in the history
fixes sveltejs#4442, fixes sveltejs#2843

- add manifest.appPath and builder.getAppPath()
- adapter-cloudflare*: use manifest.appPath instead of manifest.appDir
- adapter-netlify: use prefixed appDir for cache headers
- adapter-vercel: use prefixed appDir for routes.json
- adapter-cloudflare, adapter-cloudflare-workers, adapter-netlify: write static assets to "$dest/$base"
  • Loading branch information
hmnd committed Sep 29, 2022
1 parent 3cfbf2c commit 34d9bab
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .changeset/smooth-waves-accept.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
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;

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

builder.log.minor('Copying assets...');
builder.writeClient(site.bucket);
builder.writePrerendered(site.bucket);
const prefixedBucket = `${site.bucket}${builder.config.kit.paths.base}`;
builder.writeClient(prefixedBucket);
builder.writePrerendered(prefixedBucket);
}
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
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 prefixedDest = `${dest}${builder.config.kit.paths.base}`;
const written_files = builder.writeClient(prefixedDest);
builder.writePrerendered(prefixedDest);

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

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cloudflare/src/worker.js
Original file line number Diff line number Diff line change
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;

/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
Expand Down
7 changes: 4 additions & 3 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
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 prefixedPublish = `${publish}${builder.config.kit.paths.base}`;
builder.writeClient(prefixedPublish);
builder.writePrerendered(prefixedPublish);

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
2 changes: 1 addition & 1 deletion packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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}${
!svelte_config.kit.paths.base.endsWith('/') ? '/' : ''
}${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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 34d9bab

Please sign in to comment.