Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New pbundle option "packReduxData" for SSR redux state #1532

Merged
merged 4 commits into from
Feb 15, 2020
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
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");
});
});