Skip to content

Commit

Permalink
add is_root_relative helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Nov 23, 2021
1 parent 93ce803 commit d73a7e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mkdirp } from '../../utils/filesystem.js';
import { __fetch_polyfill } from '../../install-fetch.js';
import { SVELTE_KIT } from '../constants.js';
import { get_single_valued_header } from '../../utils/http.js';
import { resolve } from '../../utils/url.js';
import { is_root_relative, resolve } from '../../utils/url.js';

/**
* @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
Expand Down Expand Up @@ -194,7 +194,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
written_files.push(file);

const resolved = resolve(path, location);
if (resolved.startsWith('/')) {
if (is_root_relative(resolved)) {
await visit(resolved, path);
}
} else {
Expand Down Expand Up @@ -270,7 +270,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
if (!href) continue;

const resolved = resolve(path, href);
if (!resolved.startsWith('/') || resolved.startsWith('//')) continue;
if (!is_root_relative(resolved)) continue;

const parsed = new URL(resolved, 'http://localhost');
const pathname = decodeURI(parsed.pathname).replace(config.kit.paths.base, '');
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/page/load_node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { normalize } from '../../load.js';
import { respond } from '../index.js';
import { escape_json_string_in_html } from '../../../utils/escape.js';
import { resolve } from '../../../utils/url.js';
import { is_root_relative, resolve } from '../../../utils/url.js';

const s = JSON.stringify;

Expand Down Expand Up @@ -127,7 +127,7 @@ export async function load_node({
`http://${page.host}/${asset.file}`,
/** @type {RequestInit} */ (opts)
);
} else if (resolved.startsWith('/') && !resolved.startsWith('//')) {
} else if (is_root_relative(resolved)) {
const relative = resolved;

const headers = /** @type {import('types/helper').RequestHeaders} */ ({
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ export function resolve(base, path) {

return `${prefix}${baseparts.join('/')}`;
}

/** @param {string} path */
export function is_root_relative(path) {
return path[0] === '/' && path[1] !== '/';
}

0 comments on commit d73a7e2

Please sign in to comment.