Skip to content

Commit

Permalink
New pbundle option "packReduxData" for SSR redux state (#1532)
Browse files Browse the repository at this point in the history
* New pbundle option "packReduxData" for SSR redux state

* Verify attachInitialState flag before serializing packReduxData

* Add unit test for packReduxData

* Run packReduxData even if the store has already been realized
  • Loading branch information
christianlent authored Feb 15, 2020
1 parent 8671f35 commit 35eca3b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
18 changes: 14 additions & 4 deletions packages/subapp-pbundle/lib/framework-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ class FrameworkLib {
}

this.initialState = reduxData.initialState || reduxData;
// if subapp didn't request to skip sending initial state, then stringify it
// and attach it to the index html.
const packReduxData = subAppServer.packReduxData || subApp.packReduxData;

if (subAppServer.attachInitialState !== false) {
// if subapp didn't request to skip sending initial state and packReduxData was not specified,
// then stringify the initial store state and attach it to the index html.
if (!packReduxData && subAppServer.attachInitialState !== false) {
this.initialStateStr = JSON.stringify(this.initialState);
} else {
this.initialStateStr = "";
Expand Down Expand Up @@ -168,11 +169,20 @@ class FrameworkLib {
}

async realizeReduxStore() {
const { subApp, subAppServer } = this.ref;

if (this.store && this.store.realize) {
this.store = this.store.realize();
await this.signalStoreReady();
}
}

const packReduxData = subAppServer.packReduxData || subApp.packReduxData;
// if subapp didn't request to skip sending initial state and packReduxData was specified,
// then stringify packReduxData's return value and attach it to the index html.
if (packReduxData && !subAppServer.attachInitialState) {
this.initialStateStr = JSON.stringify(packReduxData(this.store));
}
}

async signalStoreReady() {
const reduxStoreReady =
Expand Down
42 changes: 41 additions & 1 deletion packages/subapp-pbundle/test/spec/ssr-framework.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,15 @@ describe("SSR Preact framework", function() {

const helloBundle = {
name: "hello",
reducer(state = "foo") {
reducer(state = "foo", action) {
if (action.type === "UPDATE_STATE") {
return action.newState;
}
return state;
},
doUpdateState: (newState) => ({ dispatch }) => {
dispatch({ type: "UPDATE_STATE", newState });
},
selectHello(state) {
return state.hello;
}
Expand Down Expand Up @@ -354,4 +360,38 @@ describe("SSR Preact framework", function() {
expect(store).to.equal(false);
expect(html).to.equal("");
});

it("should use packReduxData to generate initial state if it exists", async () => {
const framework = new lib.FrameworkLib({
subApp: {
__redux: true,
packReduxData: (store) => store.getState(),
reduxStoreReady: ({ store }) => {
store.doUpdateState("universe");
},
reduxCreateStore(initialState) {
return {
realize() {
return composeBundles(helloBundle)(initialState);
}
};
}
},
subAppServer: {
StartComponent: connect("selectHello", ({ hello }) => {
return `test hello ${hello}`;
}),
async prepare() {
return { hello: "world" };
}
},
context: {
user: {}
},
options: { serverSideRendering: true }
});

await framework.handleSSR();
expect(JSON.parse(framework.initialStateStr).hello).to.equal("universe");
});
});

0 comments on commit 35eca3b

Please sign in to comment.