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

replace request.origin/path/query with request.url #3126

Merged
merged 40 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
94b0939
replace request.origin/path/query with request.url
Rich-Harris Dec 29, 2021
f17bcbf
fix some stuff
Rich-Harris Dec 29, 2021
941b3b6
typo
Rich-Harris Dec 29, 2021
55fa638
lint
Rich-Harris Dec 29, 2021
92094b1
fix test
Rich-Harris Dec 29, 2021
5c15f7e
fix xss vuln
Rich-Harris Dec 29, 2021
2bc4cd5
fix test
Rich-Harris Dec 29, 2021
39f25b1
gah
Rich-Harris Dec 29, 2021
145aaae
fixes
Rich-Harris Dec 29, 2021
9f454bf
simplify
Rich-Harris Dec 29, 2021
0a6e6cf
use pathname+search as key
Rich-Harris Dec 29, 2021
4511c61
all tests passing
Rich-Harris Dec 29, 2021
bd496f3
lint
Rich-Harris Dec 29, 2021
c581a24
tidy up
Rich-Harris Dec 29, 2021
109108f
update types in docs
Rich-Harris Dec 29, 2021
c74cfe7
update docs
Rich-Harris Dec 29, 2021
c9a2794
more docs
Rich-Harris Dec 29, 2021
921e572
more docs
Rich-Harris Dec 29, 2021
a1ae42d
more docs
Rich-Harris Dec 29, 2021
0d70de6
i would be lost without conduitry
Rich-Harris Dec 29, 2021
28d5c1e
update template app
Rich-Harris Dec 29, 2021
b12931f
throw useful error when trying to access path/query/origin/page
Rich-Harris Dec 29, 2021
bc8c0ce
$params -> $route.params, only use getters in dev, remove the word "d…
Rich-Harris Dec 29, 2021
c83cad6
update docs
Rich-Harris Dec 29, 2021
f9e80fe
finish updating load input section
Rich-Harris Dec 29, 2021
efacc75
update
Rich-Harris Dec 29, 2021
310c10a
make this section less verbose
Rich-Harris Dec 29, 2021
3a104fa
move url into $route
Rich-Harris Dec 29, 2021
1ac14f2
update some docs
Rich-Harris Dec 29, 2021
3d6a30f
rename route store back to page
Rich-Harris Dec 29, 2021
0a53d92
throw errors on accessing $page.path etc
Rich-Harris Dec 29, 2021
761ab4c
fix types
Rich-Harris Dec 29, 2021
fc4caa1
fix some docs
Rich-Harris Dec 29, 2021
76376f4
fix migrating docs
Rich-Harris Dec 29, 2021
77030b3
decode path, strip prefix
Rich-Harris Dec 29, 2021
f0b6825
tidy up
Rich-Harris Dec 29, 2021
b1aa476
remove comment
Rich-Harris Dec 29, 2021
a12cfda
lint
Rich-Harris Dec 29, 2021
8f2c909
Update documentation/docs/05-modules.md
Rich-Harris Dec 30, 2021
2126c04
Update documentation/migrating/04-pages.md
Rich-Harris Dec 30, 2021
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
3 changes: 2 additions & 1 deletion examples/hn.svelte.dev/src/routes/[list]/[page].svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script context="module">
const valid_lists = new Set(['news', 'newest', 'show', 'ask', 'jobs']);

export async function load({ page: { params }, fetch }) {
/** @type {import('@sveltejs/kit').Load} */
export async function load({ params, fetch }) {
const list = params.list === 'top' ? 'news' : params.list === 'new' ? 'newest' : params.list;

if (!valid_lists.has(list)) {
Expand Down
13 changes: 6 additions & 7 deletions examples/hn.svelte.dev/src/routes/__layout.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
<script>
import { page, navigating } from '$app/stores';
import { url, navigating } from '$app/stores';
import Nav from '$lib/Nav.svelte';
import PreloadingIndicator from '$lib/PreloadingIndicator.svelte';
import ThemeToggler from '$lib/ThemeToggler.svelte';
import '../app.css';


$: section = $page.path.split('/')[1];
$: section = $url.pathname.split('/')[1];
</script>

<Nav {section}/>
<Nav {section} />

{#if $navigating}
<PreloadingIndicator/>
<PreloadingIndicator />
{/if}

<main>
<slot></slot>
<slot />
</main>

<ThemeToggler/>
<ThemeToggler />

<style>
main {
Expand Down
5 changes: 3 additions & 2 deletions examples/hn.svelte.dev/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script context="module">
export function load({ page }) {
let host = page.host;
/** @type {import('@sveltejs/kit').Load} */
export function load({ url }) {
let host = url.host;
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const i = host.indexOf(':');
if (i >= 0) {
host = host.substring(0, i);
Expand Down
18 changes: 11 additions & 7 deletions examples/hn.svelte.dev/src/routes/item/[id].svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script context="module">
export async function load({ page, fetch }) {
const res = await fetch(`https://api.hnpwa.com/v0/item/${page.params.id}.json`);
/** @type {import('@sveltejs/kit').Load} */
export async function load({ params, fetch }) {
const res = await fetch(`https://api.hnpwa.com/v0/item/${params.id}.json`);
const item = await res.json();

return { props: { item } };
Expand All @@ -24,7 +25,10 @@
{#if item.domain}<small>{item.domain}</small>{/if}
</a>

<p class="meta">{item.points} points by <a href="/user/{item.user}">{item.user}</a> {item.time_ago}</p>
<p class="meta">
{item.points} points by <a href="/user/{item.user}">{item.user}</a>
{item.time_ago}
</p>

{#if item.content}
{@html item.content}
Expand All @@ -33,7 +37,7 @@

<div class="comments">
{#each item.comments as comment}
<Comment comment='{comment}'/>
<Comment {comment} />
{/each}
</div>
</div>
Expand All @@ -44,13 +48,13 @@
}

.item {
border-bottom: 1em solid rgba(0,0,0,0.1);
border-bottom: 1em solid rgba(0, 0, 0, 0.1);
margin: 0 -2em 2em -2em;
padding: 0 2em 2em 2em;
}

:global(html).dark .item {
border-bottom: 1em solid rgba(255,255,255,0.1);;
border-bottom: 1em solid rgba(255, 255, 255, 0.1);
}

.main-link {
Expand All @@ -72,4 +76,4 @@
.comments > :global(.comment):first-child {
border-top: none;
}
</style>
</style>
11 changes: 6 additions & 5 deletions examples/hn.svelte.dev/src/routes/user/[name].svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script context="module">
export const hydrate = false;

export async function load({ page, fetch }) {
const res = await fetch(`https://api.hnpwa.com/v0/user/${page.params.name}.json`);
/** @type {import('@sveltejs/kit').Load} */
export async function load({ params, fetch }) {
const res = await fetch(`https://api.hnpwa.com/v0/user/${params.name}.json`);
const user = await res.json();

return {
props: {
name: page.params.name,
name: params.name,
user
}
};
Expand Down Expand Up @@ -39,7 +40,7 @@

{#if user.about}
<div class="about">
{@html '<p>' + user.about }
{@html '<p>' + user.about}
</div>
{/if}
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { page } from '$app/stores';
import { url } from '$app/stores';
import logo from './svelte-logo.svg';
</script>

Expand All @@ -15,9 +15,13 @@
<path d="M0,0 L1,2 C1.5,3 1.5,3 2,3 L2,0 Z" />
</svg>
<ul>
<li class:active={$page.path === '/'}><a sveltekit:prefetch href="/">Home</a></li>
<li class:active={$page.path === '/about'}><a sveltekit:prefetch href="/about">About</a></li>
<li class:active={$page.path === '/todos'}><a sveltekit:prefetch href="/todos">Todos</a></li>
<li class:active={$url.pathname === '/'}><a sveltekit:prefetch href="/">Home</a></li>
<li class:active={$url.pathname === '/about'}>
<a sveltekit:prefetch href="/about">About</a>
</li>
<li class:active={$url.pathname === '/todos'}>
<a sveltekit:prefetch href="/todos">Todos</a>
</li>
</ul>
<svg viewBox="0 0 2 3" aria-hidden="true">
<path d="M0,0 L0,3 C0.5,3 0.5,3 1,2 L2,0 Z" />
Expand Down
10 changes: 4 additions & 6 deletions packages/kit/src/core/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,10 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const rendered = await app.render(
{
url: `${config.kit.protocol || 'sveltekit'}://${config.kit.host || 'prerender'}${path}`,
method: 'GET',
headers: {},
path,
rawBody: null,
query: new URLSearchParams()
rawBody: null
},
{
prerender: {
Expand Down Expand Up @@ -330,11 +329,10 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
if (fallback) {
const rendered = await app.render(
{
url: `${config.kit.host || 'sveltekit'}://${config.kit.host || 'prerender'}/[fallback]`,
Conduitry marked this conversation as resolved.
Show resolved Hide resolved
method: 'GET',
headers: {},
path: '[fallback]', // this doesn't matter, but it's easiest if it's a string
rawBody: null,
query: new URLSearchParams()
rawBody: null
},
{
prerender: {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class App {
: 'default_protocol'
};

return respond({ ...request, origin: protocol + '://' + host }, this.options, { prerender });
return respond({ ...request, url: new URL(request.url, protocol + '://' + host) }, this.options, { prerender });
}
}
`;
Expand Down
14 changes: 9 additions & 5 deletions packages/kit/src/core/create_app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function generate_app(manifest_data) {
while (l--) {
pyramid = `
{#if components[${l + 1}]}
<svelte:component this={components[${l}]} {...(props_${l} || {})}>
<svelte:component this={components[${l}]} {...(props_${l} || {})}>
${pyramid.replace(/\n/g, '\n\t\t\t\t\t')}
</svelte:component>
{:else}
Expand All @@ -148,22 +148,26 @@ function generate_app(manifest_data) {

// stores
export let stores;
export let page;
export let url;
export let params;

export let components;
${levels.map((l) => `export let props_${l} = null;`).join('\n\t\t\t')}

setContext('__svelte__', stores);

$: stores.page.set(page);
afterUpdate(stores.page.notify);
$: stores.url.set(url);
afterUpdate(stores.url.notify);

$: stores.params.set(params);
afterUpdate(stores.params.notify);

let mounted = false;
let navigated = false;
let title = null;

onMount(() => {
const unsubscribe = stores.page.subscribe(() => {
const unsubscribe = stores.url.subscribe(() => {
if (mounted) {
navigated = true;
title = document.title || 'untitled page';
Expand Down
6 changes: 1 addition & 5 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,11 @@ async function create_plugin(config, dir, https, get_manifest) {
return res.end(err.reason || 'Invalid request body');
}

const origin = `${https ? 'https' : 'http'}://${req.headers.host}`;

const rendered = await respond(
{
url: new URL(`${https ? 'https' : 'http'}://${req.headers.host}${req.url}`),
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
method: req.method,
origin,
path: parsed.pathname.replace(config.kit.paths.base, ''),
query: parsed.searchParams,
rawBody: body
},
{
Expand Down
5 changes: 2 additions & 3 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ export async function preview({
const parsed = new URL(initial_url, 'http://localhost/');

const rendered =
parsed.pathname.startsWith(config.kit.paths.base) &&
initial_url.startsWith(config.kit.paths.base) &&
(await app.render({
url: `${use_https ? 'https' : 'http'}://${req.headers.host}${initial_url}`,
method: req.method,
headers: /** @type {import('types/helper').RequestHeaders} */ (req.headers),
path: parsed.pathname.replace(config.kit.paths.base, ''),
query: parsed.searchParams,
rawBody: body
}));

Expand Down
22 changes: 17 additions & 5 deletions packages/kit/src/runtime/app/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ export const getStores = () => {
const stores = getContext('__svelte__');

return {
page: {
subscribe: stores.page.subscribe
url: {
subscribe: stores.url.subscribe
},
params: {
subscribe: stores.params.subscribe
},
navigating: {
subscribe: stores.navigating.subscribe
Expand All @@ -38,11 +41,20 @@ export const getStores = () => {
};
};

/** @type {typeof import('$app/stores').page} */
export const page = {
/** @type {typeof import('$app/stores').url} */
export const url = {
/** @param {(value: any) => void} fn */
subscribe(fn) {
const store = getStores().url;
return store.subscribe(fn);
}
};

/** @type {typeof import('$app/stores').url} */
export const params = {
/** @param {(value: any) => void} fn */
subscribe(fn) {
const store = getStores().page;
const store = getStores().params;
return store.subscribe(fn);
}
};
Expand Down
Loading