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

Fix Redirect issue when using baseURL #1584

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
21 changes: 12 additions & 9 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ function handle_error(error) {
/**
* @param {number} port
* @param {boolean} https
* @param {string} base
*/
async function launch(port, https) {
async function launch(port, https, base) {
const { exec } = await import('child_process');
let cmd = 'open';
if (process.platform == 'win32') {
Expand All @@ -66,7 +67,7 @@ async function launch(port, https) {
cmd = 'xdg-open';
}
}
exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}`);
exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}${base}`);
}

const prog = sade('svelte-kit').version('__VERSION__');
Expand All @@ -83,6 +84,7 @@ prog

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = await get_config();
const { base } = config.kit.paths;

const { dev } = await import('./core/dev/index.js');

Expand All @@ -97,7 +99,7 @@ prog
process.stderr.write(data);
});

welcome({ port, host, https, open });
welcome({ port, host, https, open, base });
} catch (error) {
handle_error(error);
}
Expand Down Expand Up @@ -150,13 +152,13 @@ prog

process.env.NODE_ENV = process.env.NODE_ENV || 'production';
const config = await get_config();

const { base } = config.kit.paths;
const { preview } = await import('./core/preview/index.js');

try {
await preview({ port, host, config, https });

welcome({ port, host, https, open });
welcome({ port, host, https, open, base });
} catch (error) {
handle_error(error);
}
Expand Down Expand Up @@ -220,10 +222,11 @@ async function check_port(port) {
* host: string;
* https: boolean;
* port: number;
* base: string;
* }} param0
*/
function welcome({ port, host, https, open }) {
if (open) launch(port, https);
function welcome({ port, host, https, open, base }) {
if (open) launch(port, https, base);

console.log(colors.bold().cyan(`\n SvelteKit v${'__VERSION__'}\n`));

Expand All @@ -237,12 +240,12 @@ function welcome({ port, host, https, open }) {

// prettier-ignore
if (details.internal) {
console.log(` ${colors.gray('local: ')} ${protocol}//${colors.bold(`localhost:${port}`)}`);
console.log(` ${colors.gray('local: ')} ${protocol}//${colors.bold(`localhost:${port}${base}`)}`);
} else {
if (details.mac === '00:00:00:00:00:00') return;

if (exposed) {
console.log(` ${colors.gray('network:')} ${protocol}//${colors.bold(`${details.address}:${port}`)}`);
console.log(` ${colors.gray('network:')} ${protocol}//${colors.bold(`${details.address}:${port}${base}`)}`);
} else {
console.log(` ${colors.gray('network: not exposed')}`);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ test("fails if paths.base ends with '/'", () => {
}, /^kit\.paths\.base option must be a root-relative path that starts but doesn't end with '\/'. See https:\/\/kit\.svelte\.dev\/docs#configuration-paths$/);
});

test('fails if paths.base ends with /', () => {
assert.throws(() => {
validate_config({
kit: {
paths: {
base: '/basePath/'
}
}
});
}, /^config\.kit\.paths\.base must not end with \/$/);
});

test('fails if prerender.pages are invalid', () => {
assert.throws(() => {
validate_config({
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class Watcher extends EventEmitter {
const [merged_config, conflicts] = deep_merge(modified_vite_config, {
configFile: false,
root: this.cwd,
// Adding this fixes some tests but breaks others.
// base: `${this.config.kit.paths.base}/`,
resolve: {
alias: Array.isArray(alias)
? [
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export async function preview({
});

app.init({
// This didn't have any impact
// paths: config.kit.paths,
paths: {
base: '',
assets: '/.'
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/options/source/pages/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'uvu/assert';

/** @type {import('test').TestMaker} */
export default function (test, is_dev) {
test('serves /', '/', async ({ page, js }) => {
test('serves /', '/base-path/', async ({ page, js }) => {
assert.equal(await page.textContent('h1'), 'I am in the template');
assert.equal(await page.textContent('h2'), "We're on index.svelte");
assert.equal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'uvu/assert';

/** @type {import('test').TestMaker} */
export default function (test) {
test('enables floc', '/headers/', async ({ response }) => {
test('enables floc', '/base-path/headers/', async ({ response }) => {
const headers = response.headers();
assert.equal(headers['permissions-policy'], undefined);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/options/source/pages/host/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'uvu/assert';

/** @type {import('test').TestMaker} */
export default function (test) {
test('sets host', '/host/', async ({ page }) => {
test('sets host', '/base-path/host/', async ({ page }) => {
assert.equal(await page.textContent('[data-source="load"]'), 'example.com');
assert.equal(await page.textContent('[data-source="store"]'), 'example.com');
assert.equal(await page.textContent('[data-source="endpoint"]'), 'example.com');
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/test/apps/options/source/pages/slash/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import * as assert from 'uvu/assert';

/** @type {import('test').TestMaker} */
export default function (test) {
test('adds trailing slash', '/slash', async ({ base, page, clicknav }) => {
assert.equal(page.url(), `${base}/slash/`);
assert.equal(await page.textContent('h2'), '/slash/');
test('adds trailing slash', '/base-path/slash', async ({ base, page, clicknav }) => {
assert.equal(page.url(), `${base}/base-path/slash/`);
assert.equal(await page.textContent('h2'), '/base-path/slash/');

await clicknav('[href="/slash/child"]');
assert.equal(page.url(), `${base}/slash/child/`);
assert.equal(await page.textContent('h2'), '/slash/child/');
assert.equal(page.url(), `${base}/base-path/slash/child/`);
assert.equal(await page.textContent('h2'), '/base-path/slash/child/');
});
}
3 changes: 3 additions & 0 deletions packages/kit/test/apps/options/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const config = {
routes: 'source/pages',
template: 'source/template.html'
},
paths: {
base: '/base-path'
},
appDir: '_wheee',
floc: true,
target: '#content-goes-here',
Expand Down