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 #4442, fixes #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 Jul 22, 2022
1 parent 3a0c210 commit d449b20
Show file tree
Hide file tree
Showing 15 changed files with 34 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 { prefix } = 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);

builder.writeClient(dest);
builder.writePrerendered(dest);
const prefixedDest = `${dest}${builder.config.kit.paths.base}`;
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
8 changes: 5 additions & 3 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ 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.getPrefixedAppDirectory()}/immutable/*\n cache-control: public\n cache-control: immutable\n cache-control: max-age=31536000\n`
);

// for esbuild, use ESM
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.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 @@ -121,6 +121,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)];
},
Expand Down
Empty file.
Empty file.
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 @@ -65,6 +65,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
1 change: 1 addition & 0 deletions packages/kit/src/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export async function dev(vite, vite_config, svelte_config) {

manifest = {
appDir: svelte_config.kit.appDir,
prefix: svelte_config.kit.appDir,
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
mimeTypes: get_mime_lookup(manifest_data),
_: {
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ function kit() {
log.info(`Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`);

log.info('Building server');
const assets_prefix = `${svelte_config.kit.paths.assets || svelte_config.kit.paths.base}/${svelte_config.kit.appDir}/`;
const options = {
cwd,
config: svelte_config,
Expand All @@ -301,6 +302,7 @@ function kit() {
/** @type {import('types').BuildData} */
build_data = {
app_dir: svelte_config.kit.appDir,
prefix: assets_prefix,
manifest_data,
service_worker: options.service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
client,
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 @@ -45,6 +45,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 @@ -273,6 +274,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 @@ -44,6 +44,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 d449b20

Please sign in to comment.