使用链接模块的Next.js页面无法通过CSS导入加载文件

reactjs React.js

老丝阿飞

2020-03-23

我正在将next.js(7.0.2)添加到现有项目中,无法弄清楚为什么Link模块不加载具有的页面import 'style.css'

我遵循了@ zeit / next-css指令以及Next.js / Express集成。

我安装了以下软件包

  • 下一个7.0.2
  • @ zeit / next-css
  • css-loader@1.0.1
  • 快报(v4)

next.config.js

    // ./next.config.js file
    const withCSS = require('@zeit/next-css')
    module.exports = withCSS() 

index.js

    // ./pages/index.js file
    import Link from 'next/link'
    const Index = () => (
      <div>
        <p>Hello Next.js</p>
        <p>
          <Link href="/test"><a>test</a></Link>
        </p>
      </div>
    )

    export default Index

test.js

    // ./pages/test.js file
    import "../style.css"
    export default () => <div className="example">Hello World!</div>

style.css

    // ./style.css file
      .example {
        font-size: 50px;
      }

预期的行为:链接模块将使用CSS加载页面,就像加载其他页面一样。

实际行为:除具有的页面外,所有页面都将加载import 'my.css'我可以直接通过网址加载该页面。

EG-

  1. 作品=加载http:// localhost:3000 / index
  2. Works =加载http:// localhost:3000 / test(并应用CSS)
  3. Does NOT work = Load http://localhost:3000/index and click the test link. Nothing happens. If I go to the Network tab on the Developer Tools (Chrome) I see test.js listed and the initiator is page-loader.js. This seems to be followed by a series of on-demand-entries-ping?page=/ but not sure what it means.

Update: If I comment out the import "../style.css" the file loads (without CSS). So it seems there is something in the css processing with server side routing that isn't right with my setup.

@Darryl - here are the relevant snippets from my express initialization file. The entire file can be found (current iteration) at https://github.com/tagyoureit/nodejs-poolController/blob/6.0-DEV/src/lib/comms/server.js#L33.

  function startServerAsync(type) {
    ...
    const _next = require('next')
    const dev = process.env.NODE_ENV !== 'production'
    ...
    // servers is an object that holds http/https/socketio/mdns and other servers
    // type='http' in current testing
    servers[type].next = _next({ dev }) 
    servers[type].next.prepare()
      .then(() => {
        servers[type].app = express();
        servers[type].server = 
        container.http.createServer(servers[type].app);
        configExpressServer(servers[type].app, express, servers[type].next);
        servers[type].server = servers[type].server.listen(servers[type].port, function () {
        container.logger.verbose('Express Server ' + type + ' listening at port %d', servers[type].port);
        container.io.init(servers[type].server, type)
            resolve('Server ' + type + ' started.');
      });
    }

    // assign static/api routes
    function configExpressServer(app, express, _next) {
      // use next as router
      const handle = _next.getRequestHandler()

      // many app.get / app.use statements
      ...
      // catch all to route through next
      app.get('*', (req, res) => {
        const { parse } = require('url')
        const parsedUrl = parse(req.url, true)
        const { pathname, query } = parsedUrl
        handle(req, res, parsedUrl)
      })
  }

第3041篇《使用链接模块的Next.js页面无法通过CSS导入加载文件》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

0个回答

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android