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.prefix and builder.getAppPrefixDirectory()
- adapter-cloudflare*: use manifest.prefix 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 Apr 9, 2022
1 parent 01221d3 commit a0d6518
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 14 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 @@ -4,7 +4,7 @@ import { getAssetFromKV } from '@cloudflare/kv-asset-handler';

const server = new Server(manifest);

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

addEventListener('fetch', (/** @type {FetchEvent} */ event) => {
event.respondWith(handle(event));
Expand Down
8 changes: 5 additions & 3 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ export default function () {
writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));

builder.log.minor('Copying assets...');
builder.writeClient(bucket);
builder.writeStatic(bucket);
builder.writePrerendered(bucket);

const prefixedBucket = `${bucket}${builder.config.kit.paths.base}`;
builder.writeClient(prefixedBucket);
builder.writeStatic(prefixedBucket);
builder.writePrerendered(prefixedBucket);
}
};
}
Expand Down
7 changes: 4 additions & 3 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export default function (options = {}) {
builder.rimraf(tmp);
builder.mkdirp(tmp);

builder.writeStatic(dest);
builder.writeClient(dest);
builder.writePrerendered(dest);
const prefixedDest = `${dest}${builder.config.kit.paths.base}`;
builder.writeStatic(prefixedDest);
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 { prefix } = manifest;

/** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */
const worker = {
Expand Down
10 changes: 6 additions & 4 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ export default function ({ split = false } = {}) {
}

builder.log.minor('Copying assets...');
builder.writeStatic(publish);
builder.writeClient(publish);
builder.writePrerendered(publish);

const prefixedPublish = `${publish}${builder.config.kit.paths.base}`;
builder.writeStatic(prefixedPublish);
builder.writeClient(prefixedPublish);
builder.writePrerendered(prefixedPublish);

builder.log.minor('Writing redirects...');
const redirect_file = join(publish, '_redirects');
Expand All @@ -136,7 +138,7 @@ export default function ({ split = false } = {}) {
builder.copy('_headers', headers_file);
appendFileSync(
headers_file,
`\n\n/${builder.config.kit.appDir}/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
`\n\n${builder.getPrefixedAppDirectory()}*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
);
}
};
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 @@ -162,7 +162,7 @@ export default function ({ external = [] } = {}) {
...prerendered_pages,
...prerendered_redirects,
{
src: `/${builder.config.kit.appDir}/.+`,
src: `/${builder.getPrefixedAppDirectory()}/.+`,
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 @@ -119,6 +119,10 @@ export function create_builder({ config, build_data, prerendered, log }) {
return config.kit.files.assets;
},

getPrefixedAppDirectory() {
return build_data.prefix;
},

writeClient(dest) {
return copy(`${config.kit.outDir}/output/client`, dest, {
filter: (file) => file[0] !== '.'
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function build(config, { log }) {

const { manifest_data } = sync.all(config);

const assets_prefix = `${config.kit.paths.assets || config.kit.paths.base}/${config.kit.appDir}/`;
const options = {
cwd,
config,
Expand All @@ -34,7 +35,7 @@ export async function build(config, { log }) {
// during `svelte-kit preview`, because we use a local asset path. If Vite
// used relative paths, I _think_ this could get fixed. Issue here:
// https://github.com/vitejs/vite/issues/2009
assets_base: `${config.kit.paths.assets || config.kit.paths.base}/${config.kit.appDir}/`,
assets_base: assets_prefix,
manifest_data,
output_dir,
client_entry_file: path.relative(cwd, `${get_runtime_path(config)}/client/start.js`),
Expand All @@ -48,6 +49,7 @@ export async function build(config, { log }) {
/** @type {import('types').BuildData} */
const build_data = {
app_dir: config.kit.appDir,
prefix: assets_prefix,
manifest_data: options.manifest_data,
service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
client,
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export async function create_plugin(config, cwd) {

manifest = {
appDir: config.kit.appDir,
prefix: config.kit.appDir,
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
mimeTypes: get_mime_lookup(manifest_data),
_: {
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 @@ -63,6 +63,7 @@ export function generate_manifest({ build_data, relative_path, routes, format =
// prettier-ignore
return `{
appDir: ${s(build_data.app_dir)},
prefix: ${s(build_data.prefix)},
assets: new Set(${s(assets)}),
mimeTypes: ${s(get_mime_lookup(build_data.manifest_data))},
_: {
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface Builder {
getClientDirectory(): string;
getServerDirectory(): string;
getStaticDirectory(): string;
getPrefixedAppDirectory(): string;

/**
* @param dest the destination folder to which files should be copied
Expand Down Expand Up @@ -241,6 +242,7 @@ export class Server {

export interface SSRManifest {
appDir: string;
prefix: 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 @@ -43,6 +43,7 @@ export interface Asset {

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

0 comments on commit a0d6518

Please sign in to comment.