-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
[docs-infra] Migrate simpler modules from docs to mui-docs #47897
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
5 commits
Select commit
Hold shift + click to select a range
32f79a1
[code-infra] Migrate simpler modules from docs to mui-docs
brijeshb42 2d027c2
Create pnpm catalog for docs/mui-docs
brijeshb42 f55394a
Move GoogleAnalytics and AnalyticsProvider to @mui/docs
brijeshb42 1fe50e4
Move _document to mui-docs and make styled-components setup optional
brijeshb42 e803128
Migrate BrandingCssVarsProvider
brijeshb42 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
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
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
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 |
|---|---|---|
| @@ -1,208 +1,15 @@ | ||
| import * as React from 'react'; | ||
| import Script from 'next/script'; | ||
| import { documentGetInitialProps } from '@mui/material-nextjs/v13-pagesRouter'; | ||
| import { ServerStyleSheet } from 'styled-components'; | ||
| import Document, { Html, Head, Main, NextScript } from 'next/document'; | ||
| import GlobalStyles from '@mui/material/GlobalStyles'; | ||
| import MuiInitColorSchemeScript from '@mui/material/InitColorSchemeScript'; | ||
| import NextDocument from 'next/document'; | ||
| import { Document as MuiDocsDocument, createGetInitialProps } from '@mui/docs/Document'; | ||
| import JoyInitColorSchemeScript from '@mui/joy/InitColorSchemeScript'; | ||
| import { pathnameToLanguage } from 'docs/src/modules/utils/helpers'; | ||
| import createEmotionCache from 'docs/src/createEmotionCache'; | ||
| import { getMetaThemeColor } from '@mui/docs/branding'; | ||
| import { fontClasses } from './_app'; | ||
|
|
||
| const PRODUCTION_GA = | ||
| process.env.DEPLOY_ENV === 'production' || process.env.DEPLOY_ENV === 'staging'; | ||
| export default class MuiDocument extends NextDocument { | ||
| static getInitialProps = createGetInitialProps({ setupStyledComponents: true }); | ||
|
|
||
| const GOOGLE_ANALYTICS_ID_V4 = PRODUCTION_GA ? 'G-5NXDQLC2ZK' : 'G-XJ83JQEK7J'; | ||
| const APOLLO_TRACKING_ID = PRODUCTION_GA ? '691c2e920c5e20000d7801b6' : 'dev-id'; | ||
|
|
||
| export default class MyDocument extends Document { | ||
| render() { | ||
| const { canonicalAsServer, userLanguage } = this.props; | ||
|
|
||
| return ( | ||
| <Html lang={userLanguage} data-mui-color-scheme="light" data-joy-color-scheme="light"> | ||
| <Head> | ||
| {/* | ||
| manifest.json provides metadata used when your web app is added to the | ||
| homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/ | ||
| */} | ||
| <link rel="manifest" href="/static/manifest.json" /> | ||
| {/* PWA primary color */} | ||
| <meta | ||
| name="theme-color" | ||
| content={getMetaThemeColor('light')} | ||
| media="(prefers-color-scheme: light)" | ||
| /> | ||
| <meta | ||
| name="theme-color" | ||
| content={getMetaThemeColor('dark')} | ||
| media="(prefers-color-scheme: dark)" | ||
| /> | ||
| <link rel="icon" href="/static/favicon.ico" /> | ||
| {/* iOS Icon */} | ||
| <link rel="apple-touch-icon" sizes="180x180" href="/static/icons/180x180.png" /> | ||
| {/* SEO */} | ||
| <link | ||
| rel="canonical" | ||
| href={`https://mui.com${ | ||
| userLanguage === 'en' ? '' : `/${userLanguage}` | ||
| }${canonicalAsServer}`} | ||
| /> | ||
| <link rel="alternate" href={`https://mui.com${canonicalAsServer}`} hrefLang="x-default" /> | ||
| <GlobalStyles | ||
| styles={{ | ||
| // First SSR paint | ||
| '.only-light-mode': { | ||
| display: 'block', | ||
| }, | ||
| '.only-dark-mode': { | ||
| display: 'none', | ||
| }, | ||
| // Post SSR Hydration | ||
| '.mode-dark .only-light-mode': { | ||
| display: 'none', | ||
| }, | ||
| '.mode-dark .only-dark-mode': { | ||
| display: 'block', | ||
| }, | ||
| // TODO migrate to .only-dark-mode to .only-dark-mode-v2 | ||
| '[data-mui-color-scheme="light"] .only-dark-mode-v2': { | ||
| display: 'none', | ||
| }, | ||
| '[data-mui-color-scheme="dark"] .only-light-mode-v2': { | ||
| display: 'none', | ||
| }, | ||
| '.plan-pro, .plan-premium': { | ||
| display: 'inline-block', | ||
| height: '0.9em', | ||
| width: '1em', | ||
| verticalAlign: 'middle', | ||
| marginLeft: '0.3em', | ||
| marginBottom: '0.08em', | ||
| backgroundSize: 'contain', | ||
| backgroundRepeat: 'no-repeat', | ||
| flexShrink: 0, | ||
| }, | ||
| '.plan-pro': { | ||
| backgroundImage: 'url(/static/x/pro.svg)', | ||
| }, | ||
| '.plan-premium': { | ||
| backgroundImage: 'url(/static/x/premium.svg)', | ||
| }, | ||
| }} | ||
| /> | ||
| </Head> | ||
| <body className={fontClasses}> | ||
| <MuiInitColorSchemeScript defaultMode="system" /> | ||
| <JoyInitColorSchemeScript defaultMode="system" /> | ||
| <Main /> | ||
| <script | ||
| // eslint-disable-next-line react/no-danger | ||
| dangerouslySetInnerHTML={{ | ||
| __html: ` | ||
| window.dataLayer = window.dataLayer || []; | ||
| function gtag(){dataLayer.push(arguments);} | ||
| window.gtag = gtag; | ||
|
|
||
| ${/* Set default consent to denied (Google Consent Mode v2) */ ''} | ||
| gtag('consent', 'default', { | ||
| 'ad_storage': 'denied', | ||
| 'ad_user_data': 'denied', | ||
| 'ad_personalization': 'denied', | ||
| 'analytics_storage': 'denied', | ||
| 'wait_for_update': 500 | ||
| }); | ||
| gtag('set', 'ads_data_redaction', true); | ||
| gtag('set', 'url_passthrough', true); | ||
|
|
||
| gtag('js', new Date()); | ||
| gtag('config', '${GOOGLE_ANALYTICS_ID_V4}', { | ||
| send_page_view: false, | ||
| }); | ||
|
|
||
| ${/* Apollo initialization - called by AnalyticsProvider when consent is granted */ ''} | ||
| window.initApollo = function() { | ||
| if (window.apolloInitialized) return; | ||
| window.apolloInitialized = true; | ||
| var n = Math.random().toString(36).substring(7), | ||
| o = document.createElement('script'); | ||
| o.src = 'https://assets.apollo.io/micro/website-tracker/tracker.iife.js?nocache=' + n; | ||
| o.async = true; | ||
| o.defer = true; | ||
| o.onload = function () { | ||
| window.trackingFunctions.onLoad({ appId: '${APOLLO_TRACKING_ID}' }); | ||
| }; | ||
| document.head.appendChild(o); | ||
| }; | ||
|
|
||
| ${/* Check localStorage for existing consent and initialize if already granted */ ''} | ||
| (function() { | ||
| try { | ||
| var consent = localStorage.getItem('docs-cookie-consent'); | ||
| if (consent === 'analytics') { | ||
| window.initApollo(); | ||
| } | ||
| } catch (e) {} | ||
| })(); | ||
| `, | ||
| }} | ||
| /> | ||
| {/** | ||
| * A better alternative to <script async>, to delay its execution | ||
| * https://developer.chrome.com/blog/script-component/ | ||
| */} | ||
| <Script | ||
| strategy="afterInteractive" | ||
| src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_ANALYTICS_ID_V4}`} | ||
| /> | ||
| <NextScript /> | ||
| </body> | ||
| </Html> | ||
| <MuiDocsDocument {...this.props}> | ||
| <JoyInitColorSchemeScript defaultMode="system" /> | ||
| </MuiDocsDocument> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| MyDocument.getInitialProps = async (ctx) => { | ||
| const styledComponentsSheet = new ServerStyleSheet(); | ||
|
|
||
| try { | ||
| const finalProps = await documentGetInitialProps(ctx, { | ||
| emotionCache: createEmotionCache(), | ||
| plugins: [ | ||
| { | ||
| // styled-components | ||
| enhanceApp: (App) => (props) => styledComponentsSheet.collectStyles(<App {...props} />), | ||
| resolveProps: async (initialProps) => ({ | ||
| ...initialProps, | ||
| styles: [styledComponentsSheet.getStyleElement(), ...initialProps.styles], | ||
| }), | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // All the URLs should have a leading /. | ||
| // This is missing in the Next.js static export. | ||
| let url = ctx.req.url; | ||
| if (url[url.length - 1] !== '/') { | ||
| url += '/'; | ||
| } | ||
|
|
||
| return { | ||
| ...finalProps, | ||
| canonicalAsServer: pathnameToLanguage(url).canonicalAsServer, | ||
| userLanguage: ctx.query.userLanguage || 'en', | ||
| styles: [ | ||
| <style id="material-icon-font" key="material-icon-font" />, | ||
| <style id="font-awesome-css" key="font-awesome-css" />, | ||
| ...finalProps.emotionStyleTags, | ||
| <style id="app-search" key="app-search" />, | ||
| <style id="prismjs" key="prismjs" />, | ||
| ...React.Children.toArray(finalProps.styles), | ||
| ], | ||
| }; | ||
| } finally { | ||
| styledComponentsSheet.seal(); | ||
| } | ||
| }; |
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.
The thing is that modules like
GoogleAnalyticsare not really used outside of _app.js. And there are several others. But _app.js in X and core are almost the same code. Shouldn't we rather extract aDocsAppcomponent we can use in the _app.js of both X and core and which abstracts all the shared logic?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.
Even with this
DocsApp, we'd anyways need to move the files to the package from docs. So now that the first step is done, it'd be easier to create a DocsApp since most of its import already live in@mui/docsThere 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.
Yes, but we wouldn't need to export them from
@mui/docs. Which would reduce the API surface that needs to be maintained from this package.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.
We can refactor that during app migration. I can update it to have static export paths instead of current glob.
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.
ok