Skip to content

Commit 500e84f

Browse files
authored
Merge pull request #22 from react18-tools/backward-compatibility
Backward compatibility
2 parents a9c93ff + 5f3d46d commit 500e84f

27 files changed

+186
-109
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,25 @@ Check out examples for advanced usage.
7777

7878
### With Next.js `app` router (Server Components)
7979

80-
Update your `app/layout.jsx` to add `ThemeSwitcher` from `react18-themes`, and `NextJsSSRThemeSwitcher` from `react18-themes/server`. `NextJsSSRThemeSwitcher` is required to avoid flash of un-themed content on reload.
80+
#### Prefer static generation over SSR - No wrapper component
81+
82+
> If your app is mostly serving static content, you do not want the overhead of SSR. Use `NextJsSSGThemeSwitcher` in this case.
83+
> When using this approach, you need to use CSS general sibling Combinator (~) to make sure your themed CSS is properly applied. See (HTML & CSS)[#html--css].
84+
85+
Update your `app/layout.jsx` to add `ThemeSwitcher` from `react18-themes`, and `NextJsSSGThemeSwitcher` from `react18-themes/server`. `NextJsSSGThemeSwitcher` is required to avoid flash of un-themed content on reload.
8186

8287
```tsx
8388
// app/layout.jsx
8489
import { ThemeSwitcher } from "react18-themes";
85-
import { NextJsSSRThemeSwitcher } from "react18-themes/server/nextjs";
90+
import { NextJsSSGThemeSwitcher } from "react18-themes/server/nextjs";
8691

8792
export default function Layout({ children }) {
8893
return (
8994
<html lang="en">
9095
<head />
9196
<body>
92-
/** use NextJsSSRThemeSwitcher as first element inside body */
93-
<NextJsSSRThemeSwitcher />
97+
/** use NextJsSSGThemeSwitcher as first element inside body */
98+
<NextJsSSGThemeSwitcher />
9499
<ThemeSwitcher />
95100
{children}
96101
</body>
@@ -101,11 +106,13 @@ export default function Layout({ children }) {
101106

102107
Woohoo! You just added multiple theme modes and you can also use Server Component! Isn't that awesome!
103108

104-
#### Version 1 (Legacy)
109+
#### Prefer SSR over SSG - Use wrapper component
110+
111+
> If your app is serving dynamic content and you want to utilize SSR, continue using `ServerSideWrapper` component to replace `html` tag in `layout.tsx` file.
105112
106113
Update your `app/layout.jsx` to add `ThemeSwitcher` and `ServerSideWrapper` from `react18-themes`. `ServerSideWrapper` is required to avoid flash of un-themed content on reload.
107114

108-
```js
115+
```tsx
109116
// app/layout.jsx
110117
import { ThemeSwitcher } from "react18-themes";
111118
import { ServerSideWrapper } from "react18-themes/server/nextjs";
@@ -141,7 +148,7 @@ That's it, your Next.js app fully supports dark mode, including System preferenc
141148
--foreground: white;
142149
}
143150

144-
// v2 onwards when using React18 server components, we need to use CSS Combinators
151+
// v2 onwards when using NextJsSSGThemeSwitcher, we need to use CSS Combinators
145152
[data-theme="dark"] ~ * {
146153
--background: black;
147154
--foreground: white;
@@ -212,14 +219,14 @@ Forcing color scheme will apply your defaultDark or defaultLight theme, configur
212219

213220
#### Motivation:
214221

215-
For server side syncing, we need to use cookies and headers. This means that this component and its children can not be static. They will be rendered server side for each request. Thus, we are avoiding the wrapper. Now, only the `NextJsSSRThemeSwitcher` will be rendered server side for each request and rest of your app can be server statically.
222+
For server side syncing, we need to use cookies and headers. This means that this component and its children can not be static. They will be rendered server side for each request. Thus, we are avoiding the wrapper. Now, only the `NextJsSSGThemeSwitcher` will be rendered server side for each request and rest of your app can be server statically.
216223

217224
Take care of the following while migrating to `v2`.
218225

219-
- No changes required for projects not using `Next.js` app router or server components.
226+
- No changes required for projects not using `Next.js` app router or server components other than updating cookies policy if needed.
220227
- The persistent storage is realized with `cookies` in place of `localStorage`. (You might want to update cookies policy accordingly.)
221-
- `ServerSideWrapper` for `Next.js` is rebranded to `NextJsSSRThemeSwitcher`. No longer need to use this as a wrapper.
222-
- Visit [With Next.js `app` router (Server Components)](<#with-next.js-app-router-(server-components)>)
228+
- We have provided `NextJsSSGThemeSwitcher` in addition to `ServerSideWrapper` for `Next.js`. You no longer need to use a wrapper component which broke static generation and forced SSR.
229+
- Visit [With Next.js `app` router (Server Components)](#with-nextjs-app-router-server-components)
223230

224231
## Migrating from v0 to v1
225232

examples/nextjs/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# nextjs-example
22

3+
## 0.0.10
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [6b46438]
8+
9+
10+
311
## 0.0.9
412

513
### Patch Changes

examples/nextjs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ To learn more about Next.js, take a look at the following resources:
1919
- [Next.js Documentation](https://nextjs.org/docs) - Learn about Next.js features and API.
2020
- [Learn Next.js](https://www.udemy.com/course/react-and-next-js-with-typescript/?referralCode=7202184A1E57C3DCA8B2) - an interactive Next.js course.
2121

22-
You can check out [the Turbo Template GitHub repository](https://github.com/mayank1513/turborepo-template/) - your feedback and contributions are welcome!
22+
You can check out [the Turbo Template GitHub repository](https://github.com/react18-tools/turborepo-template/) - your feedback and contributions are welcome!
2323

2424
### 🤩 Don't forget to start this repo!
2525

examples/nextjs/app/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ForcedPage } from "react18-themes/server";
2-
import { NextJsSSRThemeSwitcher } from "react18-themes/server";
2+
import { NextJsSSGThemeSwitcher } from "react18-themes/server";
33
import { Inter } from "next/font/google";
44
import { SharedRootLayout, darkThemes, lightThemes } from "shared-ui";
55
import Link from "next/link";
@@ -15,7 +15,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }):
1515
return (
1616
<html lang="en">
1717
<body>
18-
<NextJsSSRThemeSwitcher forcedPages={forcedPages} />
18+
<NextJsSSGThemeSwitcher forcedPages={forcedPages} />
1919
<SharedRootLayout LinkElement={Link} className={inter.className}>
2020
{children}
2121
</SharedRootLayout>

examples/nextjs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nextjs-example",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"private": true,
55
"scripts": {
66
"dev": "next dev --port 3002",
@@ -18,7 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"@next/eslint-plugin-next": "^14.0.3",
21-
"@types/node": "^20.10.0",
21+
"@types/node": "^20.10.1",
2222
"@types/react": "^18.2.39",
2323
"@types/react-dom": "^18.2.17",
2424
"eslint-config-custom": "workspace:*",

examples/remix/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
### Patch Changes
66

7+
- Updated dependencies [6b46438]
8+
9+
10+
11+
## null
12+
13+
### Patch Changes
14+
715
- Updated dependencies [f3704f9]
816
917

examples/vite/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# vite-example
22

3+
## 0.0.10
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [6b46438]
8+
9+
10+
311
## 0.0.9
412

513
### Patch Changes

examples/vite/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ To learn more about Next.js, take a look at the following resources:
1616
- [Next.js Documentation](https://nextjs.org/docs) - Learn about Next.js features and API.
1717
- [Learn Next.js](https://www.udemy.com/course/react-and-next-js-with-typescript/?referralCode=7202184A1E57C3DCA8B2) - an interactive Next.js course.
1818

19-
You can check out [the Turbo Template GitHub repository](https://github.com/mayank1513/turborepo-template/) - your feedback and contributions are welcome!
19+
You can check out [the Turbo Template GitHub repository](https://github.com/react18-tools/turborepo-template/) - your feedback and contributions are welcome!
2020

2121
### 🤩 Don't forget to start this repo!
2222

examples/vite/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-example",
33
"private": true,
4-
"version": "0.0.9",
4+
"version": "0.0.10",
55
"type": "module",
66
"scripts": {
77
"dev": "vite --port 3001",
@@ -27,6 +27,6 @@
2727
"eslint-plugin-react-hooks": "^4.6.0",
2828
"eslint-plugin-react-refresh": "^0.4.4",
2929
"typescript": "^5.3.2",
30-
"vite": "^5.0.3"
30+
"vite": "^5.0.4"
3131
}
3232
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
"tsconfig": "workspace:*",
1515
"turbo": "^1.10.16"
1616
},
17-
"packageManager": "pnpm@8.6.10",
18-
"name": "react18-themes"
17+
"packageManager": "pnpm@8.9.0",
18+
"name": "react18-themes-turborepo"
1919
}

0 commit comments

Comments
 (0)