-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(home): fix server-side rendering (#695)
- Loading branch information
Showing
14 changed files
with
653 additions
and
1,437 deletions.
There are no files selected for viewing
This file contains 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 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 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,9 +1,5 @@ | ||
import React from "react" | ||
|
||
import Svg from "../assets/share/img/icons/safari-pinned-tab.svg" | ||
|
||
export const Logo: React.FC<{ className?: string }> = (props) => ( | ||
<div {...props}> | ||
<Svg alt="Rino" width="128" height="128" /> | ||
</div> | ||
) | ||
export const Logo: React.FC<{ className?: string }> = (props) => { | ||
return <img src="/share/img/icons/safari-pinned-tab.svg" alt="Rino Logo" {...props} /> | ||
} |
This file contains 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,11 +1,30 @@ | ||
/* istanbul ignore file */ | ||
|
||
import { AppProps } from "next/app" | ||
import Head from "next/head" | ||
import React from "react" | ||
|
||
import { BaseApp } from "@rino.app/next/dist/app" | ||
const BaseApp: React.FC<AppProps> = (props) => { | ||
const { Component, pageProps } = props | ||
|
||
// https://github.com/mui-org/material-ui/blob/v4.12.1/examples/nextjs/pages/_app.js#L11-L17 | ||
React.useEffect(() => { | ||
// Remove the server-side injected CSS. | ||
const jssStyles = document.querySelector("#jss-server-side") | ||
if (jssStyles) { | ||
jssStyles.parentElement.removeChild(jssStyles) | ||
} | ||
}, []) | ||
|
||
export default function MyApp<T>({ Component, pageProps }: { Component: React.FC<T>; pageProps: T }) { | ||
return ( | ||
<BaseApp> | ||
<React.Fragment> | ||
<Head> | ||
<meta name="viewport" content="initial-scale=1.0, width=device-width" /> | ||
<title>Rino</title> | ||
</Head> | ||
<Component {...pageProps} /> | ||
</BaseApp> | ||
</React.Fragment> | ||
) | ||
} | ||
|
||
export default BaseApp |
This file contains 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,15 +1,83 @@ | ||
import Document from "next/document" | ||
/* istanbul ignore file */ | ||
|
||
import { getNextDocument, getNextDocumentInitialProps } from "@rino.app/next/dist/document" | ||
import { ServerStyleSheets } from "@material-ui/core/styles" | ||
import Document, { DocumentContext, Head, Html, Main, NextScript } from "next/document" | ||
import React from "react" | ||
|
||
class MyDocument extends Document { | ||
import { NextMeta } from "@rino.app/next" | ||
|
||
class BaseDocument extends Document { | ||
render() { | ||
return getNextDocument({ host: "https://www.rino.app", hasManifest: false }) | ||
const host = "https://www.rino.app" | ||
const hasManifest = false | ||
const description = "WYSIWYG Markdown Editor" | ||
|
||
return ( | ||
<Html lang="en"> | ||
<Head> | ||
<NextMeta hasManifest={hasManifest} host={host} description={description} /> | ||
</Head> | ||
<body> | ||
<noscript> | ||
<strong> | ||
We're sorry but Rino doesn't work properly without JavaScript enabled. Please enable it to continue. | ||
</strong> | ||
</noscript> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
} | ||
|
||
// copied from https://github.com/mui-org/material-ui/blob/v4.12.1/examples/nextjs/pages/_document.js#L27-L68 | ||
// | ||
// `getInitialProps` belongs to `_document` (instead of `_app`), | ||
// it's compatible with server-side generation (SSG). | ||
export async function getNextDocumentInitialProps(ctx: DocumentContext) { | ||
// Resolution order | ||
// | ||
// On the server: | ||
// 1. app.getInitialProps | ||
// 2. page.getInitialProps | ||
// 3. document.getInitialProps | ||
// 4. app.render | ||
// 5. page.render | ||
// 6. document.render | ||
// | ||
// On the server with error: | ||
// 1. document.getInitialProps | ||
// 2. app.render | ||
// 3. page.render | ||
// 4. document.render | ||
// | ||
// On the client | ||
// 1. app.getInitialProps | ||
// 2. page.getInitialProps | ||
// 3. app.render | ||
// 4. page.render | ||
|
||
// Render app and page and get the context of the page with collected side effects. | ||
const sheets = new ServerStyleSheets() | ||
const originalRenderPage = ctx.renderPage | ||
|
||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />), | ||
}) | ||
|
||
const initialProps = await Document.getInitialProps(ctx) | ||
|
||
return { | ||
...initialProps, | ||
// Styles fragment is rendered after the app and page rendering finish. | ||
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()], | ||
} | ||
} | ||
|
||
MyDocument.getInitialProps = async (ctx) => { | ||
BaseDocument.getInitialProps = async (ctx) => { | ||
return await getNextDocumentInitialProps(ctx) | ||
} | ||
|
||
export default MyDocument | ||
export default BaseDocument |
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 @@ | ||
export * from "./meta" |
Oops, something went wrong.