Skip to content

Commit eedaa2d

Browse files
DanDroryAujahredhopewilliamlark
authored
Match vite render API to the webpack api (#1222)
Co-authored-by: Jahred <[email protected]> Co-authored-by: William Lark <[email protected]>
1 parent 92a7ea2 commit eedaa2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+482
-675
lines changed

.changeset/lovely-houses-spend.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sku': patch
3+
---
4+
5+
`vite`: Change render entrypoint file `api` to match that of `webpack` rendering

docs/docs/vite-support.md

+5-72
Original file line numberDiff line numberDiff line change
@@ -28,81 +28,14 @@ The following commands are supported:
2828

2929
- `build` static site generation.
3030
- `start` start the development server for static site generation.
31-
- `start-ssr` start the development server for server side rendering.
3231

33-
### Static rendering
32+
### Rendering
3433

35-
Before starting with `vite` static rendering make sure you've read the [static rendering](./docs/static-rendering.md) documentation for webpack.
34+
Before starting with `vite` rendering make sure you've read the [static rendering](./docs/static-rendering.md) documentation for webpack.
3635

37-
### Client entrypoint
38-
39-
The only difference between the webpack and the vite client entrypoint is the name of the root element. In the future, this change will not be necessary.
40-
41-
```diff
42-
import { hydrateRoot } from 'react-dom/client';
43-
import { BrowserRouter } from 'react-router';
44-
45-
import { App } from './App';
46-
47-
export default ({ site }: { site: string }) => {
48-
hydrateRoot(
49-
+ document.getElementById('root')!,
50-
- document.getElementById('app')!,
51-
<BrowserRouter>
52-
<App site={site} />
53-
</BrowserRouter>,
54-
);
55-
};
56-
```
57-
58-
### Render entrypoint
59-
60-
The render entrypoint file has to export a `ViteRender` object. The `ViteRender` object has two functions: `render` and `provideClientContext`.
61-
62-
#### render
63-
64-
The `render` function should return your application as a pipeable stream (using `React.renderToPipeableString`).
65-
66-
`render` will be called once for each combination of settings in sku config. Specifically, `environment`, `site` & `route`.
67-
68-
If you are using `loadable` components, you should wrap your application in a `LoadableProvider` from `sku/vite/loadable`. This will search your rendered application for dynamic imports that are wrapped in a `loadable` function.
69-
By calling the `await preloadAll()` function, you can ensure that all `loadable` imported files will be preloaded before sku renders the page.
70-
71-
#### provideClientContext
72-
73-
The `provideClientContext` is the same as that of the webpack render entrypoint. See [provideClientContext](./docs/static-rendering.md#provideclientcontext) for more information.
74-
75-
**Example of a render entrypoint with `vite`**
76-
77-
```tsx
78-
import { StaticRouter } from 'react-router-dom/server';
79-
import type { ViteRender } from 'sku';
80-
import { renderToPipeableStream } from 'react-dom/server';
81-
import { LoadableProvider, preloadAll } from 'sku/vite/loadable';
82-
83-
import App from './App';
84-
85-
export default {
86-
render: async ({ options, renderContext, site, url }) => {
87-
const { loadableCollector } = renderContext;
88-
89-
await preloadAll();
90-
91-
return renderToPipeableStream(
92-
<LoadableProvider value={loadableCollector}>
93-
<StaticRouter location={url} context={{}}>
94-
<App site={site} />
95-
</StaticRouter>
96-
</LoadableProvider>,
97-
options,
98-
);
99-
},
100-
101-
provideClientContext: ({ site }) => ({
102-
site,
103-
}),
104-
} satisfies ViteRender;
105-
```
36+
> [!NOTE]
37+
> There are some differences between the two renderers.
38+
> You can find a detailed explanation of the changes and how to migrate over down below.
10639
10740
### Code splitting
10841

fixtures/suspense/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@sku-fixtures/suspense",
33
"private": true,
4+
"type": "module",
45
"dependencies": {
56
"@tanstack/react-query": "^5.56.2",
67
"react": "^18.3.0",

fixtures/suspense/sku.config.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export default {
88
routes: [{ route: '/', name: 'home' }],
99
},
1010
],
11+
clientEntry: 'src/client.jsx',
12+
renderEntry: 'src/render.jsx',
1113
environments: ['production'],
1214
port: 8203,
1315
publicPath: '/static/suspense',

fixtures/suspense/sku.config.vite.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import baseConfig from './sku.config.js';
2+
3+
export default {
4+
...baseConfig,
5+
__UNSAFE_EXPERIMENTAL__bundler: 'vite',
6+
};
File renamed without changes.

fixtures/suspense/src/client.js renamed to fixtures/suspense/src/client.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { hydrateRoot } from 'react-dom/client';
2-
import { BrowserRouter } from 'react-router-dom';
31
import {
42
HydrationBoundary,
53
QueryClient,
64
QueryClientProvider,
75
} from '@tanstack/react-query';
6+
import { hydrateRoot } from 'react-dom/client';
7+
import { BrowserRouter } from 'react-router-dom';
88

99
import App from './App';
1010

fixtures/suspense/src/render.js renamed to fixtures/suspense/src/render.jsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import React from 'react';
2-
import html from 'dedent';
3-
import { StaticRouter } from 'react-router-dom/server';
41
import {
52
dehydrate,
63
QueryClient,
74
QueryClientProvider,
85
} from '@tanstack/react-query';
6+
import html from 'dedent';
7+
import { StaticRouter } from 'react-router-dom/server';
98

109
import App from './App';
1110

fixtures/translations/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.eslintcache
66
.prettierrc
77
coverage/
8-
dist-ssr/
8+
dist/
99
eslint.config.mjs
1010
report/
1111
tsconfig.json

fixtures/translations/.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ translations.ts
55
.eslintcache
66
.prettierrc
77
coverage/
8-
dist-ssr/
8+
dist/
99
eslint.config.mjs
1010
pnpm-lock.yaml
1111
report/

fixtures/translations/src/render.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { renderToString } from 'react-dom/server';
2-
import html from 'dedent';
31
import { VocabProvider } from '@vocab/react';
4-
5-
import type { RenderContext } from './types';
2+
import html from 'dedent';
3+
import { renderToString } from 'react-dom/server';
64
import type { Render } from 'sku';
5+
76
import App from './App';
7+
import type { RenderContext } from './types';
88

99
export default {
1010
renderApp: ({ SkuProvider, language }) =>
@@ -31,4 +31,4 @@ export default {
3131
</body>
3232
</html>
3333
`,
34-
} as Render;
34+
} satisfies Render;

fixtures/vite-test-app/.gitignore

-9
This file was deleted.

fixtures/vite-test-app/.prettierignore

-10
This file was deleted.

fixtures/vite-test-app/package.json

-20
This file was deleted.

fixtures/vite-test-app/sku.config.ts

-29
This file was deleted.

fixtures/vite-test-app/src/App.tsx

-52
This file was deleted.

fixtures/vite-test-app/src/client.tsx

-16
This file was deleted.

fixtures/vite-test-app/src/handlers/Details.tsx

-21
This file was deleted.

fixtures/vite-test-app/src/handlers/Home.tsx

-4
This file was deleted.

fixtures/vite-test-app/src/index.css

-3
This file was deleted.

fixtures/vite-test-app/src/render.tsx

-32
This file was deleted.

0 commit comments

Comments
 (0)