Commit 02e75cfd authored by Henrik Wenz's avatar Henrik Wenz Committed by Tim Neutkens

Simplify with-noscript example (#9094)

## Changelog

- Removed react-lazyload
- Removed images
- Removed ReactDOMServer from Client
- Removed next.config.js

## Notes

**CommonJS vs ESM:** In the future we might be able to use top level `await` in order to import only on the server (e.g.: `await import(“react-dom/server”)`)

Until then we need to mix CommonJS and ESM in favor of messing with the webpack config.
parent beed775e
import React from 'react' import React from 'react'
import ReactDOMServer from 'react-dom/server'
export default function Noscript (props) { // We don't want to send 'react-dom/server' to the client
const staticMarkup = ReactDOMServer.renderToStaticMarkup(props.children) let ReactDOMServer
if (typeof window === 'undefined') {
ReactDOMServer = require('react-dom/server')
}
export default function Noscript ({ children }) {
if (!ReactDOMServer) {
return null
}
const staticMarkup = ReactDOMServer.renderToStaticMarkup(children)
return <noscript dangerouslySetInnerHTML={{ __html: staticMarkup }} /> return <noscript dangerouslySetInnerHTML={{ __html: staticMarkup }} />
} }
module.exports = {
webpack: (config, { dev }) => {
if (!dev) {
config.resolve.alias = {
'react-dom/server': require.resolve(
'react-dom/umd/react-dom-server.browser.production.min.js'
)
}
}
return config
}
}
...@@ -9,8 +9,7 @@ ...@@ -9,8 +9,7 @@
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^16.7.0",
"react-dom": "^16.7.0", "react-dom": "^16.7.0"
"react-lazyload": "^2.2.7"
}, },
"license": "ISC" "license": "ISC"
} }
import React from 'react' import React from 'react'
import LazyLoad from 'react-lazyload'
import Noscript from '../components/Noscript' import Noscript from '../components/Noscript'
const images = [ export default function IndexPage () {
'/static/img/reactjs.png', return (
'/static/img/nextjs.png', <>
'/static/img/vuejs.png', <h1>noscript</h1>
'/static/img/angular.jpg' <p>Disable JavaScript to see it in action:</p>
]
class Index extends React.Component { <hr />
static getInitialProps (context) {
const { isServer } = context
return { isServer }
}
render () {
return (
<div style={{ textAlign: 'center' }}>
{images.map((item, index) => (
<div key={index}>
<LazyLoad height={700} offset={100}>
<img width={700} height={700} src={item} alt={`image_${index}`} />
</LazyLoad>
<Noscript>
<img width={700} height={700} src={item} alt={`image_${index}`} />
</Noscript>
</div>
))}
</div>
)
}
}
export default Index <Noscript>Noscript is enabled!</Noscript>
</>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment