最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Did not expect server HTML to contain a <div> in <main> - Stack Overflow

programmeradmin3浏览0评论

I'm working in a project that uses:

  • react/[email protected]
  • @loadable/ponent
  • styled-ponents
  • react-router-dom

The application renders both server side and client side.

I'm using @loadable/ponent to dynamically code split this way.

router.tsx

import * as React from 'react'
import loadable from '@loadable/ponent'
import { Route, Switch } from 'react-router-dom'

const NotFound = loadable(() =>
  import('../ponents/NotFound/NotFound' /* webpackChunkName: "notfound" */)
)

const routes = (
  <Switch>
    <Route ponent={NotFound} />
  </Switch>
)

export default routes

When loading the page, this error appear on the console and the page seems to flick for a second.

react-dom.development.js:546 Warning: Did not expect server HTML to contain a <div> in <main>.

When I check the output in both sides (server/client), they are identical.

When I remove @loadable/ponent like bellow, it works and the error is gone.

router-without-loadable.tsx

import * as React from 'react'
import { Route, Switch } from 'react-router-dom'
import NotFound from '../ponents/NotFound/NotFound'

const routes = (
  <Switch>
    <Route ponent={NotFound} />
  </Switch>
)

export default routes

Seems to be something to do with @loadable/ponent but I'm not 100% sure.

I'm working in a project that uses:

  • react/[email protected]
  • @loadable/ponent
  • styled-ponents
  • react-router-dom

The application renders both server side and client side.

I'm using @loadable/ponent to dynamically code split this way.

router.tsx

import * as React from 'react'
import loadable from '@loadable/ponent'
import { Route, Switch } from 'react-router-dom'

const NotFound = loadable(() =>
  import('../ponents/NotFound/NotFound' /* webpackChunkName: "notfound" */)
)

const routes = (
  <Switch>
    <Route ponent={NotFound} />
  </Switch>
)

export default routes

When loading the page, this error appear on the console and the page seems to flick for a second.

react-dom.development.js:546 Warning: Did not expect server HTML to contain a <div> in <main>.

When I check the output in both sides (server/client), they are identical.

When I remove @loadable/ponent like bellow, it works and the error is gone.

router-without-loadable.tsx

import * as React from 'react'
import { Route, Switch } from 'react-router-dom'
import NotFound from '../ponents/NotFound/NotFound'

const routes = (
  <Switch>
    <Route ponent={NotFound} />
  </Switch>
)

export default routes

Seems to be something to do with @loadable/ponent but I'm not 100% sure.

Share edited Sep 1, 2019 at 14:32 Alejandro Garcia Anglada asked Aug 30, 2019 at 11:01 Alejandro Garcia AngladaAlejandro Garcia Anglada 2,4031 gold badge28 silver badges41 bronze badges 2
  • @loadable is used for SSR meaning your pages are piled at server side and sent as HTML to client side – Rikin Commented Aug 30, 2019 at 11:16
  • Yes, well the HTML is not send to the client, is sent to the browser as a response. But yeah my problem is along the lines of dynamic imports. – Alejandro Garcia Anglada Commented Aug 30, 2019 at 11:22
Add a ment  | 

2 Answers 2

Reset to default 5

Finally have an answer for this:

  1. For @loadable/ponent to work properly, you need to put the magic webpack ment (/* webpackChunkName: "notfound" */) before the path of the file this way.
const NotFound = loadable(() =>
  import(/* webpackChunkName: "notfound" */ '../ponents/NotFound/NotFound')
)

Reference:

https://github./smooth-code/loadable-ponents/issues/23

  1. And more important, in the server side, you need to wrap you app in a ChunkExtractorManager and pass the client extractor (I was passing the server extractor, it's not very clear in the docs).
const statsFile = path.resolve('./wwwroot/dist/loadable-stats.json')
const extractor = new ChunkExtractor({ 
  statsFile, 
  entrypoints: ['client'] // name of the proper webpack endpoint (default: main)
})

<ChunkExtractorManager extractor={extractor}>
  <App />
</ChunkExtractorManager>

Here is a proper clear example on how to implement it:

https://github./iamssen/seed

Update 24.09.2019

Added to the official docs

https://www.smooth-code./open-source/loadable-ponents/docs/server-side-rendering/#chunkextractor-entrypoints

I think the problem is your NotFound ponent is not loaded and thus Route dont know what to render which is causing the error.

You would need to modify something like below:

<Route path="/404/" exact ponent={props => <NotFound {...props} />} />
发布评论

评论列表(0)

  1. 暂无评论