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

dynamic base #9150

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/adapter-static/test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'fs';
import fs from 'node:fs';
import * as assert from 'uvu/assert';
import { run } from './utils.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ function kit({ svelte_config }) {
const { assets, base } = svelte_config.kit.paths;

if (browser) {
return `export const base = ${s(base)};
return `export const base = ${global}.base;
export const assets = ${global}.assets;`;
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module '__sveltekit/environment' {

/** Internal version of $app/paths */
declare module '__sveltekit/paths' {
export const base: `/${string}`;
export const base: string;
export let assets: `https://${string}` | `http://${string}`;
export function set_assets(path: string): void;
}
16 changes: 12 additions & 4 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ export async function render_response({
rendered = { head: '', html: '', css: { code: '', map: null } };
}

const segments = event.url.pathname.slice(base.length).split('/').slice(2);
const resolved_base = segments.map(() => '..').join('/') || '.';

// we use a relative path when possible to support IPFS, the internet archive, etc.
const base_expression =
state.prerendering?.fallback || base !== ''
? s(base)
: `new URL(${s(resolved_base)}, location.href).pathname.slice(0,-1)`;

/**
* The prefix to use for static assets. Replaces `%sveltekit.assets%` in the template
* @type {string}
Expand All @@ -176,10 +185,8 @@ export async function render_response({
resolved_assets = base;
asset_expression = s(base);
} else {
// otherwise we want asset paths to be relative to the page, so that they
// will work in odd contexts like IPFS, the internet archive, and so on
const segments = event.url.pathname.slice(base.length).split('/').slice(2);
resolved_assets = segments.length > 0 ? segments.map(() => '..').join('/') : '.';
resolved_assets = resolved_base;
// we use a relative path when possible to support IPFS, the internet archive, etc.
asset_expression = `new URL(${s(
resolved_assets
)}, location.href).pathname.replace(/^\\\/$/, '')`;
Expand Down Expand Up @@ -286,6 +293,7 @@ export async function render_response({
const properties = [
`env: ${s(public_env)}`,
`assets: ${asset_expression}`,
`base: ${base_expression}`,
`element: document.currentScript.parentElement`
];

Expand Down