-
Notifications
You must be signed in to change notification settings - Fork 85
Support SSR streaming #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| "use client"; | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
|
||
| import React, { useMemo, useEffect } from "react"; | ||
| import type { ReactNode } from "react"; | ||
| import { isBrowser } from "../tools/isBrowser"; | ||
| import { SsrIsDarkProvider } from "../useIsDark/server"; | ||
| import type { DefaultColorScheme } from "./zz_internal/defaultColorScheme"; | ||
| import { setUseLang } from "../i18n"; | ||
| import { setLink } from "../link"; | ||
| import { start } from "../start"; | ||
|
|
||
| export type DsfrProviderProps = { | ||
| children: ReactNode; | ||
| lang: string | undefined; | ||
| /** Default: false */ | ||
| verbose?: boolean; | ||
| /** | ||
| * When true, the nonce of the script tag will be checked, fetched from {@link DsfrHead} component and injected in react-dsfr scripts. | ||
| * | ||
| * @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce | ||
| * @default false | ||
| */ | ||
| doCheckNonce?: boolean; | ||
| /** | ||
| * Enable Trusted Types with a custom policy name. | ||
| * | ||
| * Don't forget to also add the policy name in {@link DsfrHead} component. | ||
| * | ||
| * `<trustedTypesPolicyName>` and `<trustedTypesPolicyName>-asap` should be set in your Content-Security-Policy header. | ||
| * | ||
| * For example: | ||
| * ```txt | ||
| * With a policy name of "react-dsfr": | ||
| * Content-Security-Policy: | ||
| * require-trusted-types-for 'script'; | ||
| * trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler; | ||
| * ``` | ||
| * | ||
| * @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types | ||
| * @see {@link DEFAULT_TRUSTED_TYPES_POLICY_NAME} | ||
| * @default "react-dsfr" | ||
| */ | ||
| trustedTypesPolicyName?: string; | ||
| }; | ||
|
|
||
| export function DsfrProviderBase( | ||
| props: DsfrProviderProps & { | ||
| Link: Function; | ||
| defaultColorScheme: DefaultColorScheme; | ||
| } | ||
| ) { | ||
| const { | ||
| children, | ||
| lang, | ||
| Link, | ||
| defaultColorScheme, | ||
| verbose = false, | ||
| doCheckNonce = false, | ||
| trustedTypesPolicyName = "react-dsfr" | ||
| } = props; | ||
|
|
||
| /* | ||
| useEffect(() => { | ||
| dsfrEffect(); | ||
| }, []); | ||
| */ | ||
|
|
||
| useMemo(() => { | ||
| if (!isBrowser) { | ||
| return; | ||
| } | ||
|
|
||
| start({ | ||
| defaultColorScheme, | ||
| verbose, | ||
| doCheckNonce, | ||
| trustedTypesPolicyName, | ||
| "nextParams": { | ||
| "doPersistDarkModePreferenceWithCookie": false, | ||
| "registerEffectAction": action => { | ||
| console.log("registerEffectAction", action); | ||
|
|
||
| if (isAfterFirstEffect) { | ||
| console.log("run now"); | ||
| action(); | ||
| } else { | ||
| console.log("push"); | ||
| actions.push(action); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| }, []); | ||
|
|
||
| useMemo(() => { | ||
| if (lang === undefined) { | ||
| return; | ||
| } | ||
| setUseLang({ useLang: () => lang }); | ||
| }, [lang]); | ||
|
|
||
| useMemo(() => { | ||
| setLink({ Link: Link as any }); | ||
| }, [Link]); | ||
|
|
||
| if (isBrowser) { | ||
| return <>{children}</>; | ||
| } | ||
|
|
||
| const isDark = defaultColorScheme === "dark" ? true : false; | ||
|
|
||
| return <SsrIsDarkProvider value={isDark}>{children}</SsrIsDarkProvider>; | ||
| } | ||
|
|
||
| let isAfterFirstEffect = false; | ||
| const actions: (() => void)[] = []; | ||
|
|
||
| function dsfrEffect(): void { | ||
| if (isAfterFirstEffect) { | ||
| return; | ||
| } | ||
| isAfterFirstEffect = true; | ||
| actions.forEach(action => { | ||
| console.log("running action", action); | ||
| action(); | ||
| }); | ||
| } | ||
|
|
||
| export function StartDsfrOnHydration() { | ||
| useEffect(() => { | ||
| console.log("wesh hydratation!"); | ||
|
|
||
| dsfrEffect(); | ||
| }, []); | ||
|
|
||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { data_fr_scheme, data_fr_theme } from "../useIsDark/constants"; | ||
| import type { ColorScheme } from "../useIsDark"; | ||
| import { | ||
| type DefaultColorScheme, | ||
| setDefaultColorSchemeServerSide | ||
| } from "./zz_internal/defaultColorScheme"; | ||
| import { setUseLang } from "../i18n"; | ||
|
|
||
| const suppressHydrationWarning = true; | ||
|
|
||
| export function createGetHtmlAttributes(params: { defaultColorScheme: DefaultColorScheme }) { | ||
| const { defaultColorScheme } = params; | ||
|
|
||
| function getHtmlAttributes(params: { | ||
| lang: string | undefined; | ||
| }): { suppressHydrationWarning: true; lang?: string } & ( | ||
| | Record<typeof data_fr_scheme | typeof data_fr_theme, ColorScheme> | ||
| | {} | ||
| ) { | ||
| const { lang } = params; | ||
|
|
||
| setDefaultColorSchemeServerSide({ defaultColorScheme }); | ||
|
|
||
| if (lang !== undefined) { | ||
| setUseLang({ useLang: () => lang }); | ||
| } | ||
|
|
||
| if (defaultColorScheme === "system") { | ||
| return { | ||
| lang, | ||
| suppressHydrationWarning | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| lang, | ||
| suppressHydrationWarning, | ||
| [data_fr_scheme]: defaultColorScheme, | ||
| [data_fr_theme]: defaultColorScheme | ||
| }; | ||
| } | ||
| return { getHtmlAttributes }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| export type { RegisterLink } from "../link"; | ||
| export type { DefaultColorScheme } from "./zz_internal/defaultColorScheme"; | ||
| export { startReactDsfr } from "./zz_internal/start"; | ||
| export { DsfrProviderBase, type DsfrProviderProps, StartDsfrOnHydration } from "./DsfrProvider"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { createGetHtmlAttributes } from "./getHtmlAttributes"; | ||
| export { DsfrHeadBase, type DsfrHeadProps } from "./DsfrHead"; |
1 change: 0 additions & 1 deletion
1
...-appdir/zz_internal/defaultColorScheme.ts → ...-router/zz_internal/defaultColorScheme.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import marianneLightWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Light.woff2"; | ||
| import marianneItalicWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Light_Italic.woff2"; | ||
| import marianneRegularWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Regular.woff2"; | ||
| import marianneRegularItalicWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Regular_Italic.woff2"; | ||
| import marianneMediumWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Medium.woff2"; | ||
| import marianneMediumItalicWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Medium_Italic.woff2"; | ||
| import marianneBoldWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Bold.woff2"; | ||
| import marianneBoldItalicWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Bold_Italic.woff2"; | ||
| import spectralRegularWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Spectral-Regular.woff2"; | ||
| import spectralExtraBoldWoff2Url from "@codegouvfr/react-dsfr/dsfr/fonts/Spectral-ExtraBold.woff2"; | ||
|
|
||
| export const fontUrlByFileBasename = { | ||
| "Marianne-Light": marianneLightWoff2Url, | ||
| "Marianne-Light_Italic": marianneItalicWoff2Url, | ||
| "Marianne-Regular": marianneRegularWoff2Url, | ||
| "Marianne-Regular_Italic": marianneRegularItalicWoff2Url, | ||
| "Marianne-Medium": marianneMediumWoff2Url, | ||
| "Marianne-Medium_Italic": marianneMediumItalicWoff2Url, | ||
| "Marianne-Bold": marianneBoldWoff2Url, | ||
| "Marianne-Bold_Italic": marianneBoldItalicWoff2Url, | ||
| "Spectral-Regular": spectralRegularWoff2Url, | ||
| "Spectral-ExtraBold": spectralExtraBoldWoff2Url | ||
| } as const; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
io
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@revolunet oula merci pardon pardon!