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

Commit

Permalink
pass response object to store getter - fixes #344
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 8, 2018
1 parent 666c113 commit 444908c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}) {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 };
Expand Down
11 changes: 9 additions & 2 deletions test/app/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
4 changes: 2 additions & 2 deletions test/common/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down

0 comments on commit 444908c

Please sign in to comment.