Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

compare sliced route match to determine if segment_dirty is true #691

Merged
merged 1 commit into from
May 21, 2019
Merged
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
15 changes: 13 additions & 2 deletions runtime/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,22 @@ export async function hydrate_target(target: Target): Promise<{
let l = 1;

try {
const { pattern } = route;
const match = pattern.exec(page.path);
let segment_dirty = false;
branch = await Promise.all(route.parts.map(async (part, i) => {
const segment = segments[i];

if (current_branch[i] && current_branch[i].segment !== segment) segment_dirty = true;
if (
current_branch[i]
&& (
current_branch[i].segment !== segment
|| (
current_branch[i].match
&& JSON.stringify(current_branch[i].match.slice(1, i+2)) !== JSON.stringify(match.slice(1, i+2))
)
)
) segment_dirty = true;

props.segments[l] = segments[i + 1]; // TODO make this less confusing
if (!part) return { segment };
Expand Down Expand Up @@ -324,7 +335,7 @@ export async function hydrate_target(target: Target): Promise<{
preloaded = initial_data.preloaded[i + 1];
}

return (props[`level${j}`] = { component, props: preloaded, segment, part: part.i });
return (props[`level${j}`] = { component, props: preloaded, segment, match, part: part.i });
}));
} catch (error) {
props.error = error;
Expand Down
10 changes: 10 additions & 0 deletions test/apps/basics/src/routes/[...rest]/deep.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
<script context="module">
export function preload({ query, params }) {
const { rest } = params;
return { rest };
}
</script>

<script>
import { stores } from '@sapper/app';
const { page } = stores();
export let rest;
</script>

<h1>{$page.params.rest.join(',')}</h1>
<h2>{rest.join(',')}</h2>

<a href="xyz/abc/qwe/deep.json">deep</a>
<a href="xyz/abc">back</a>
12 changes: 12 additions & 0 deletions test/apps/basics/src/routes/[...rest]/index.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<script context="module">
export function preload({ query, params }) {
const { rest } = params;
return { rest };
}
</script>

<script>
import { stores } from '@sapper/app';
const { page } = stores();
export let rest;
</script>

<h1>{$page.params.rest.join(',')}</h1>
<h2>{rest.join(',')}</h2>

<a href="xyz/abc/deep">deep</a>
<a href="xyz/abc">deep</a>
<a href="xyz/abc/def">deep</a>
<a href="xyz/abc/def/ghi">deep</a>
24 changes: 19 additions & 5 deletions test/apps/basics/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from 'assert';
import * as http from 'http';
import { build } from '../../../api';
import { AppRunner } from '../AppRunner';
import {build} from '../../../api';
import {AppRunner} from '../AppRunner';

declare let deleted: { id: number };
declare let el: any;
Expand Down Expand Up @@ -267,7 +267,6 @@ describe('basics', function() {
await r.sapper.start();

assert.equal(await r.text('h1'), 'foo');

await r.page.click('[href="dirs/bar"]');
await r.wait();
assert.equal(await r.text('h1'), 'bar');
Expand All @@ -278,11 +277,26 @@ describe('basics', function() {
await r.sapper.start();

assert.equal(await r.text('h1'), 'abc,xyz');

await r.page.click('[href="xyz/abc/def/ghi"]');
await r.wait();
assert.equal(await r.text('h1'), 'xyz,abc,def,ghi');
assert.equal(await r.text('h2'), 'xyz,abc,def,ghi');
await r.page.click('[href="xyz/abc/def"]');
await r.wait();
assert.equal(await r.text('h1'), 'xyz,abc,def');
assert.equal(await r.text('h2'), 'xyz,abc,def');
await r.page.click('[href="xyz/abc/def"]');
await r.wait();
assert.equal(await r.text('h1'), 'xyz,abc,def');
assert.equal(await r.text('h2'), 'xyz,abc,def');
await r.page.click('[href="xyz/abc"]');
await r.wait();
assert.equal(await r.text('h1'), 'xyz,abc');
assert.equal(await r.text('h2'), 'xyz,abc');
await r.page.click('[href="xyz/abc/deep"]');
await r.wait();
assert.equal(await r.text('h1'), 'xyz,abc');

assert.equal(await r.text('h2'), 'xyz,abc');
await r.page.click('[href="xyz/abc/qwe/deep.json"]');
await r.wait();
assert.equal(
Expand Down
3 changes: 2 additions & 1 deletion test/apps/layout/src/routes/[x]/[y]/[z].svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
</script>

<span>z: {$page.params.z} {count}</span>
<a href="foo/bar/qux">click me</a>
<a href="foo/bar/qux">goto foo/bar/qux</a>
<a href="foo/abc/def">goto foo/abc/def</a>
21 changes: 18 additions & 3 deletions test/apps/layout/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('layout', function() {
assert.deepEqual(text1.split('\n').map(str => str.trim()).filter(Boolean), [
'y: bar 1',
'z: baz 1',
'click me',
'goto foo/bar/qux',
'goto foo/abc/def',
'child segment: baz'
]);

Expand All @@ -32,7 +33,8 @@ describe('layout', function() {
assert.deepEqual(text2.split('\n').map(str => str.trim()).filter(Boolean), [
'y: bar 1',
'z: baz 1',
'click me',
'goto foo/bar/qux',
'goto foo/abc/def',
'child segment: baz'
]);

Expand All @@ -43,9 +45,22 @@ describe('layout', function() {
assert.deepEqual(text3.split('\n').map(str => str.trim()).filter(Boolean), [
'y: bar 1',
'z: qux 2',
'click me',
'goto foo/bar/qux',
'goto foo/abc/def',
'child segment: qux'
]);

await r.page.click('[href="foo/abc/def"]');
await r.wait();

const text4 = await r.text('#sapper');
assert.deepEqual(text4.split('\n').map(str => str.trim()).filter(Boolean), [
'y: abc 2',
'z: def 3',
'goto foo/bar/qux',
'goto foo/abc/def',
'child segment: def'
]);
});

it('survives the tests with no server errors', () => {
Expand Down