Skip to content

Commit 1610e8e

Browse files
committed
fix
1 parent 31e07c7 commit 1610e8e

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

packages/fastify-react/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "index.js",
77
"name": "@fastify/react",
88
"description": "The official @fastify/vite renderer for React",
9-
"version": "1.0.2",
9+
"version": "1.1.0",
1010
"files": [
1111
"plugin/index.js",
1212
"plugin/parsers.js",
@@ -24,6 +24,16 @@
2424
"virtual/resource.js",
2525
"virtual/root.jsx",
2626
"virtual/routes.js",
27+
"virtual-ts/layouts/default.tsx",
28+
"virtual-ts/context.ts",
29+
"virtual-ts/core.tsx",
30+
"virtual-ts/create.tsx",
31+
"virtual-ts/index.ts",
32+
"virtual-ts/layouts.ts",
33+
"virtual-ts/mount.ts",
34+
"virtual-ts/resource.ts",
35+
"virtual-ts/root.tsx",
36+
"virtual-ts/routes.ts",
2737
"client.js",
2838
"context.js",
2939
"index.js",
@@ -53,7 +63,6 @@
5363
"react": "^19.1.0",
5464
"react-dom": "^19.1.0",
5565
"react-router": "^7.5.0",
56-
"unihead": "latest",
5766
"valtio": "latest",
5867
"youch": "^3.3.4"
5968
},

packages/fastify-react/virtual-ts/core.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { useEffect } from 'react'
33
import { BrowserRouter, StaticRouter, useLocation } from 'react-router'
44
import { proxy } from 'valtio'
55
import { RouteContext, useRouteContext } from '@fastify/react/client'
6-
import layouts from '$app/layouts.ts'
7-
import { waitFetch, waitResource } from '$app/resource.ts'
6+
import layouts from '$app/layouts.js'
7+
import { waitFetch, waitResource } from '$app/resource.js'
88

99
export const isServer = import.meta.env.SSR
1010
export const Router = isServer ? StaticRouter : BrowserRouter
@@ -33,7 +33,7 @@ export function useServerAction(action, options = {}) {
3333
return actionData[action]
3434
}
3535

36-
export function AppRoute({ head, ctxHydration, ctx, children }) {
36+
export function AppRoute({ ctxHydration, ctx, children }) {
3737
// If running on the server, assume all data
3838
// functions have already ran through the preHandler hook
3939
if (isServer) {
@@ -100,7 +100,8 @@ export function AppRoute({ head, ctxHydration, ctx, children }) {
100100
if (!ctx.firstRender && ctx.getMeta) {
101101
const updateMeta = async () => {
102102
const { getMeta } = await ctx.loader()
103-
head.update(await getMeta(ctx))
103+
ctx.head = await getMeta(ctx)
104+
ctxHydration.useHead.push(ctx.head)
104105
}
105106
waitResource(path, 'updateMeta', updateMeta)
106107
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
import Root from '$app/root.tsx'
1+
import {
2+
UnheadProvider as ClientUnheadProvider
3+
} from '@unhead/react/client'
4+
import {
5+
UnheadProvider as ServerUnheadProvider
6+
} from '@unhead/react/server?server'
7+
8+
import Root from '$app/root.jsx'
29

310
export default function create({ url, ...serverInit }) {
4-
return <Root url={url} {...serverInit} />
11+
const UnheadProvider = import.meta.env.SSR
12+
? ServerUnheadProvider
13+
: ClientUnheadProvider
14+
return (
15+
<UnheadProvider value={serverInit.ctxHydration.useHead}>
16+
<Root url={url} {...serverInit} />
17+
</UnheadProvider>
18+
)
519
}

packages/fastify-react/virtual-ts/mount.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import { createRoot, hydrateRoot } from 'react-dom/client'
2-
import Head from 'unihead/client'
32
import { hydrateRoutes } from '@fastify/react/client'
4-
import routes from '$app/routes.ts'
5-
import create from '$app/create.tsx'
6-
import * as context from '$app/context.ts'
3+
import { createHead } from '@unhead/react/client'
4+
import routes from '$app/routes.js'
5+
import create from '$app/create.jsx'
6+
import * as context from '$app/context.js'
77

88
async function mountApp (...targets) {
99
const ctxHydration = await extendContext(window.route, context)
10-
const head = new Head(window.route.head, window.document)
1110
const resolvedRoutes = await hydrateRoutes(routes)
1211
const routeMap = Object.fromEntries(
1312
resolvedRoutes.map((route) => [route.path, route]),
1413
)
14+
const useHead = createHead()
15+
ctxHydration.useHead = useHead
16+
ctxHydration.useHead.push(window.route.head)
17+
1518
const app = create({
16-
head,
1719
ctxHydration,
1820
routes: window.routes,
1921
routeMap,
2022
})
23+
24+
2125
let mountTargetFound = false
2226
for (const target of targets) {
2327
const targetElem = document.querySelector(target)

0 commit comments

Comments
 (0)