Skip to content

Commit

Permalink
docs: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 18, 2024
1 parent d05b513 commit 567d6da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
19 changes: 13 additions & 6 deletions playground/remix-flat-routes/READMD.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ i18next.changeLanguage = async (lng?: string, ...args) => {
5. 在路由变化时,重复第3步
```ts
// root.tsx
export const loader = () => null // 需要导出loader,shouldRevalidate才会生效
let url: URL

export const loader: LoaderFunction = async () => {
if (url) {
await asyncLoadResource(i18next.language, {
namespaces: resolveNamespace(url.pathname),
})
}
return null
}

export const shouldRevalidate = async ({ nextUrl, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) => {
await asyncLoadResource(i18next.language, {
namespaces: resolveNamespace(nextUrl.pathname),
})
return defaultShouldRevalidate
export const shouldRevalidate: ShouldRevalidateFunction = ({ nextUrl }: ShouldRevalidateFunctionArgs) => {
url = nextUrl
return true
}
```
22 changes: 16 additions & 6 deletions playground/remix-flat-routes/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { type PropsWithChildren } from 'react'
import { useTranslation } from 'react-i18next'
import {
Link,
type LoaderFunction,
ScrollRestoration,
type ShouldRevalidateFunction,
type ShouldRevalidateFunctionArgs,
useLocation,
useOutlet,
Expand Down Expand Up @@ -34,13 +36,21 @@ const RouteAnimation = ({ children }: PropsWithChildren) => {
)
}

export const loader = () => null
let url: URL

export const shouldRevalidate = async (args: ShouldRevalidateFunctionArgs) => {
await asyncLoadResource(i18next.language, {
namespaces: resolveNamespace(args.nextUrl.pathname),
})
return args.defaultShouldRevalidate
export const loader: LoaderFunction = async () => {
console.log(url, 'url')
if (url) {
await asyncLoadResource(i18next.language, {
namespaces: resolveNamespace(url.pathname),
})
}
return null
}

export const shouldRevalidate: ShouldRevalidateFunction = ({ nextUrl }: ShouldRevalidateFunctionArgs) => {
url = nextUrl
return true
}

export function Component() {
Expand Down
19 changes: 14 additions & 5 deletions playground/remix-ssr/app/routes/$lang+/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@ import { type ShouldRevalidateFunctionArgs } from '@remix-run/react'
import i18next from 'i18next'
import { resolveNamespace } from '@/i18n/i18n'

export const clientLoader = () => null
let url: URL

export const shouldRevalidate = async ({ nextUrl }: ShouldRevalidateFunctionArgs) => {
await window.asyncLoadResource?.(i18next.language, {
namespaces: [...resolveNamespace(nextUrl.pathname)],
})
export const loader = async () => {
console.log(url, 'url')

if (url) {
await window.asyncLoadResource?.(i18next.language, {
namespaces: resolveNamespace(url.pathname),
})
}
return null
}

export const shouldRevalidate = ({ nextUrl }: ShouldRevalidateFunctionArgs) => {
url = nextUrl
return true
}

Expand Down

0 comments on commit 567d6da

Please sign in to comment.