From b6e2736eb08469d68463aabe719ff77352095882 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Mon, 20 May 2019 23:45:39 -0400 Subject: [PATCH] always re-run preload functions when query string changes - fixes #701 --- runtime/src/app/app.ts | 14 ++++++++++++-- .../preloading/src/routes/echo/index.svelte | 17 +++++++++++++++++ test/apps/preloading/test.ts | 19 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/apps/preloading/src/routes/echo/index.svelte diff --git a/runtime/src/app/app.ts b/runtime/src/app/app.ts index 34ed6e2..cfcb6de 100644 --- a/runtime/src/app/app.ts +++ b/runtime/src/app/app.ts @@ -22,6 +22,7 @@ let root_component: Component; let current_token: {}; let root_preloaded: Promise; let current_branch = []; +let current_query = '{}'; const stores = { page: writable({}), @@ -250,11 +251,17 @@ async function render(redirect: Redirect, branch: any[], props: any, page: Page) } current_branch = branch; + current_query = JSON.stringify(page.query); ready = true; session_dirty = false; } -function part_changed(i, segment, match) { +function part_changed(i, segment, match, stringified_query) { + // TODO only check query string changes for preload functions + // that do in fact depend on it (using static analysis or + // runtime instrumentation) + if (stringified_query !== current_query) return true; + const previous = current_branch[i]; if (!previous) return false; @@ -304,12 +311,15 @@ export async function hydrate_target(target: Target): Promise<{ let l = 1; try { + const stringified_query = JSON.stringify(page.query); const match = route.pattern.exec(page.path); + let segment_dirty = false; + branch = await Promise.all(route.parts.map(async (part, i) => { const segment = segments[i]; - if (part_changed(i, segment, match)) segment_dirty = true; + if (part_changed(i, segment, match, stringified_query)) segment_dirty = true; props.segments[l] = segments[i + 1]; // TODO make this less confusing if (!part) return { segment }; diff --git a/test/apps/preloading/src/routes/echo/index.svelte b/test/apps/preloading/src/routes/echo/index.svelte new file mode 100644 index 0000000..e45cfa3 --- /dev/null +++ b/test/apps/preloading/src/routes/echo/index.svelte @@ -0,0 +1,17 @@ + + + + +
{JSON.stringify(query)}
+ +foo=1 +foo=2 +foo=3 \ No newline at end of file diff --git a/test/apps/preloading/test.ts b/test/apps/preloading/test.ts index 4aa05fb..f62db63 100644 --- a/test/apps/preloading/test.ts +++ b/test/apps/preloading/test.ts @@ -109,4 +109,23 @@ describe('preloading', function() { it('survives the tests with no server errors', () => { assert.deepEqual(r.errors, []); }); + + it('re-runs preload when page.query changes', async () => { + await r.load('/echo?foo=1'); + await r.sapper.start(); + await r.sapper.prefetchRoutes(); + + assert.equal( + await r.text('pre'), + `{"foo":"1"}` + ); + + await r.page.click('a[href="echo?foo=2"]'); + await r.wait(); + + assert.equal( + await r.text('pre'), + `{"foo":"2"}` + ); + }); });