Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit 707b80f

Browse files
committed
feat(core): add state.frontity.rendering with either ssr or csr
1 parent 51847d2 commit 707b80f

File tree

5 files changed

+29
-145
lines changed

5 files changed

+29
-145
lines changed

packages/core/src/client/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export default async ({ packages }) => {
5252
loadableReady(() => {
5353
hydrate(<App store={store} />, window.document.getElementById("root"));
5454

55+
// Switch to CSR mode.
56+
store.state.frontity.rendering = "csr";
57+
5558
// Run afterCSR actions only if we are not in a HMR reload.
5659
if (!window["frontity"]) {
5760
Object.values(store.actions).forEach(({ afterCSR }) => {

packages/core/src/server/utils/__tests__/__snapshots__/initial-state.tests.ts.snap

Lines changed: 1 addition & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`initialState should return a valid initial link 1`] = `
4-
Object {
5-
"frontity": Object {
6-
"initialLink": "/category/nature/page/2",
7-
"menu1": Array [
8-
"item1",
9-
"item2",
10-
"item3",
11-
],
12-
"mode": "html",
13-
"name": "site",
14-
"packages": Array [
15-
"package1",
16-
"package2",
17-
],
18-
"platform": "server",
19-
"prop1": "overwritten by package2",
20-
"url": "https://site.com",
21-
},
22-
"package1": Object {
23-
"prop2": "prop2",
24-
},
25-
"package2": Object {
26-
"prop3": "prop2",
27-
},
28-
}
29-
`;
30-
31-
exports[`initialState should return a valid initial link with hash 1`] = `
32-
Object {
33-
"frontity": Object {
34-
"initialLink": "/page/2#some-hash",
35-
"menu1": Array [
36-
"item1",
37-
"item2",
38-
"item3",
39-
],
40-
"mode": "html",
41-
"name": "site",
42-
"packages": Array [
43-
"package1",
44-
"package2",
45-
],
46-
"platform": "server",
47-
"prop1": "overwritten by package2",
48-
"url": "https://site.com",
49-
},
50-
"package1": Object {
51-
"prop2": "prop2",
52-
},
53-
"package2": Object {
54-
"prop3": "prop2",
55-
},
56-
}
57-
`;
58-
59-
exports[`initialState should return a valid initial link with home 1`] = `
60-
Object {
61-
"frontity": Object {
62-
"initialLink": "/",
63-
"menu1": Array [
64-
"item1",
65-
"item2",
66-
"item3",
67-
],
68-
"mode": "html",
69-
"name": "site",
70-
"packages": Array [
71-
"package1",
72-
"package2",
73-
],
74-
"platform": "server",
75-
"prop1": "overwritten by package2",
76-
"url": "https://site.com",
77-
},
78-
"package1": Object {
79-
"prop2": "prop2",
80-
},
81-
"package2": Object {
82-
"prop3": "prop2",
83-
},
84-
}
85-
`;
86-
87-
exports[`initialState should return a valid initial link with query 1`] = `
88-
Object {
89-
"frontity": Object {
90-
"initialLink": "/page/2?some=query",
91-
"menu1": Array [
92-
"item1",
93-
"item2",
94-
"item3",
95-
],
96-
"mode": "html",
97-
"name": "site",
98-
"packages": Array [
99-
"package1",
100-
"package2",
101-
],
102-
"platform": "server",
103-
"prop1": "overwritten by package2",
104-
"url": "https://site.com",
105-
},
106-
"package1": Object {
107-
"prop2": "prop2",
108-
},
109-
"package2": Object {
110-
"prop3": "prop2",
111-
},
112-
}
113-
`;
114-
115-
exports[`initialState should return a valid initial link with query and hash 1`] = `
116-
Object {
117-
"frontity": Object {
118-
"initialLink": "/page/2?some=query#some-hash",
119-
"menu1": Array [
120-
"item1",
121-
"item2",
122-
"item3",
123-
],
124-
"mode": "html",
125-
"name": "site",
126-
"packages": Array [
127-
"package1",
128-
"package2",
129-
],
130-
"platform": "server",
131-
"prop1": "overwritten by package2",
132-
"url": "https://site.com",
133-
},
134-
"package1": Object {
135-
"prop2": "prop2",
136-
},
137-
"package2": Object {
138-
"prop3": "prop2",
139-
},
140-
}
141-
`;
142-
1433
exports[`initialState should return a valid initial state 1`] = `
1444
Object {
1455
"frontity": Object {
@@ -157,6 +17,7 @@ Object {
15717
],
15818
"platform": "server",
15919
"prop1": "overwritten by package2",
20+
"rendering": "ssr",
16021
"url": "https://site.com",
16122
},
16223
"package1": Object {

packages/core/src/server/utils/__tests__/initial-state.tests.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,44 @@ describe("initialState", () => {
4747

4848
it("should return a valid initial link", () => {
4949
const url = new URL("https://site.com/category/nature/page/2");
50-
expect(initialState({ settings, url })).toMatchSnapshot();
50+
expect(initialState({ settings, url }).frontity.initialLink).toBe(
51+
"/category/nature/page/2"
52+
);
5153
});
5254

5355
it("should return a valid initial link with home", () => {
5456
const url = new URL("https://site.com");
55-
expect(initialState({ settings, url })).toMatchSnapshot();
57+
expect(initialState({ settings, url }).frontity.initialLink).toBe("/");
5658
});
5759

5860
it("should return a valid initial link with query", () => {
5961
const url = new URL("https://site.com/page/2?some=query");
60-
expect(initialState({ settings, url })).toMatchSnapshot();
62+
expect(initialState({ settings, url }).frontity.initialLink).toBe(
63+
"/page/2?some=query"
64+
);
6165
});
6266

6367
it("should return a valid initial link with hash", () => {
6468
const url = new URL("https://site.com/page/2#some-hash");
65-
expect(initialState({ settings, url })).toMatchSnapshot();
69+
expect(initialState({ settings, url }).frontity.initialLink).toBe(
70+
"/page/2#some-hash"
71+
);
6672
});
6773

6874
it("should return a valid initial link with query and hash", () => {
6975
const url = new URL("https://site.com/page/2?some=query#some-hash");
70-
expect(initialState({ settings, url })).toMatchSnapshot();
76+
expect(initialState({ settings, url }).frontity.initialLink).toBe(
77+
"/page/2?some=query#some-hash"
78+
);
79+
});
80+
81+
it("should return a ssr rendering on the server", () => {
82+
const url = new URL("https://site.com/page/2?some=query#some-hash");
83+
expect(initialState({ settings, url }).frontity.rendering).toBe("ssr");
84+
});
85+
86+
it("should return a server platform on the server", () => {
87+
const url = new URL("https://site.com/page/2?some=query#some-hash");
88+
expect(initialState({ settings, url }).frontity.platform).toBe("server");
7189
});
7290
});

packages/core/src/server/utils/initial-state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default ({
1414
name: settings.name,
1515
mode: settings.mode,
1616
platform: "server",
17+
rendering: "ssr",
1718
initialLink: `${url.pathname}${url.search}${url.hash}`,
1819
packages: settings.packages.map(pkg => pkg.name)
1920
}

packages/types/src/package.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface Package {
1414
initialLink?: string;
1515
packages?: string[];
1616
platform?: "client" | "server";
17+
rendering?: "ssr" | "csr";
1718
// Populated by the user:
1819
title?: string;
1920
description?: string;

0 commit comments

Comments
 (0)