From 444908cac534a5a339156eeb925dc089a2e16b0a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 8 Aug 2018 10:57:10 -0400 Subject: [PATCH] pass response object to store getter - fixes #344 --- src/middleware.ts | 9 ++++++--- test/app/app/server.js | 11 +++++++++-- test/common/test.js | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/middleware.ts b/src/middleware.ts index 6941ab36b..b1d0a25a1 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -83,7 +83,7 @@ function toIgnore(uri: string, val: any) { export default function middleware(opts: { manifest: Manifest, - store: (req: Req) => Store, + store: (req: Req, res: ServerResponse) => Store, ignore?: any, routes?: any // legacy }) { @@ -276,7 +276,10 @@ function get_server_route_handler(routes: ServerRoute[]) { }; } -function get_page_handler(manifest: Manifest, store_getter: (req: Req) => Store) { +function get_page_handler( + manifest: Manifest, + store_getter: (req: Req, res: ServerResponse) => Store +) { const output = locations.dest(); const get_chunks = dev() @@ -326,7 +329,7 @@ function get_page_handler(manifest: Manifest, store_getter: (req: Req) => Store) res.setHeader('Link', link); - const store = store_getter ? store_getter(req) : null; + const store = store_getter ? store_getter(req, res) : null; let redirect: { statusCode: number, location: string }; let preload_error: { statusCode: number, message: Error | string }; diff --git a/test/app/app/server.js b/test/app/app/server.js index eb36e5adf..f09d31898 100644 --- a/test/app/app/server.js +++ b/test/app/app/server.js @@ -85,11 +85,18 @@ const middlewares = [ next(); }, + // set up some values for the store + (req, res, next) => { + req.hello = 'hello'; + res.locals = { name: 'world' }; + next(); + }, + sapper({ manifest, - store: () => { + store: (req, res) => { return new Store({ - title: 'Stored title' + title: `${req.hello} ${res.locals.name}` }); }, ignore: [ diff --git a/test/common/test.js b/test/common/test.js index c46751f97..7bb2fb0e3 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -581,11 +581,11 @@ function run({ mode, basepath = '' }) { return nightmare.goto(`${base}/store`) .page.title() .then(title => { - assert.equal(title, 'Stored title'); + assert.equal(title, 'hello world'); return nightmare.init().page.title(); }) .then(title => { - assert.equal(title, 'Stored title'); + assert.equal(title, 'hello world'); }); });