Cant change page with 'next/link' & 'next-css'
This is bug report
Link does not work with css-module imported. That happens when page with Link has no css, and linked page has. No errors in console, so im not sure about reasons, but there is minimal repo to reproduce: https://github.com/standy/next-css-error
Bug appears in [email protected] + [email protected],
Older [email protected] + [email protected] works fine
I have the exact same issue, on the exact same versions as well.
Confirmed work around: make every page import some css.
Was trying hard to determine what was causing that. Thanks for reporting @Dynogic and @standy
Same problem except with next-css. Clicking links importing less just don't respond. My workaround was to stop using less/css. Note this ticket https://github.com/zeit/next.js/issues/5264#issuecomment-424000127 suggests you can import blank in _app.js.
https://spectrum.chat/thread/2183fc55-236d-42cb-92b9-3ab10acc6303?m=MTUzODU2NDg2ODA2Mg==
Next team is aware and working on it! It's complex and will take time.
Importing an empty stylesheet into _app.js nor importing a stylesheet on every page resolved the issue for me locally.
@kylemh I tried Importing an empty stylesheet into _app.js nor importing a stylesheet on every page resolved the issue for me locally. but still not working
It's a strategy some have professed, but it doesn't resolve anything for me - just like you.
Same problem
@kylemh / others could you try @zeit/next-css@canary? It holds #315
I would fuckin' love to ❤️
@timneutkens The error is gone, great job! However when changing routes I'm not getting that new route's style... is anyone else having that issue?
Unfortunately I have to go and won't be able to troubleshoot it further or provide additional details today.
^ this precisely.
It doesn't look like I'm getting more CSS files per route - just on initial load.
SSR gets all styles properly.
Same issue with @zeit/next-css@canary not get css load on route change
That being said, I'd say this issue can be closed and another can be made on route-based code splitting withCSS with CSS Modules + PostCSS (at least for me) not working as desired?
Yes, on route change issue still present. Long time i used next6 with old next-css and regulary gets not loaded styles on route change. After refresh page it works as expected even on route change. On 7 version it reproduced on every page change (not sometimes as next6). But if you 1) load first page 2) transition to next page (gets not loaded styles) 3) refresh page - then you take working styles on both pages.
Looks like styles compilation is long process and delayed and page do not have time for take it and apply. After refresh page styles already precompiled and applied immediatelly.
my workaround this bug (on latest canary)
somewhere in _app.js
import Router from 'next/router';
Router.events.on('routeChangeComplete', () => {
if (process.env.NODE_ENV !== 'production') {
const els = document.querySelectorAll('link[href*="/_next/static/css/styles.chunk.css"]');
const timestamp = new Date().valueOf();
els[0].href = '/_next/static/css/styles.chunk.css?v=' + timestamp;
}
})
on every page reloads css after router change
my workaround this bug (on latest canary)
somewhere in
_app.js... import Router from 'next/router'; ... Router.onRouteChangeComplete = () => { if (process.env.NODE_ENV !== 'production') { const els = document.querySelectorAll('link[href*="/_next/static/css/styles.chunk.css"]'); const timestamp = new Date().valueOf(); els[0].href = '/_next/static/css/styles.chunk.css?v=' + timestamp; } }on every page reloads css after router change
look like bicyclecode
@plag thanks so much for sharing that! 😄
@plag I've updated your comment to use Router.events instead: https://github.com/zeit/next.js#router-events
I had found a way to solve it. Not really perfect.
I have used a component Layout in every page.
import React, { Component } from 'react';
import Head from 'next/head';
export default class Layout extends Component {
render() {
const { children, title, style, className } = this.props;
return (
<div className={'main-layout col ' + className} style={style}>
<Head>
<title>{title}</title>
{process.env.NODE_ENV !== 'production' && (
<link rel="stylesheet" type="text/css" href={'/_next/static/css/styles.chunk.css?v=' + Date.now()} />
)}
</Head>
<div className="main-content">{children}</div>
</div>
);
}
}
I had found a way to solve it. Not really perfect.
I have used a component
Layoutin every page.import React, { Component } from 'react'; import Head from 'next/head'; export default class Layout extends Component { render() { const { children, title, style, className } = this.props; return ( <div className={'main-layout col ' + className} style={style}> <Head> <title>{title}</title> {process.env.NODE_ENV !== 'production' && ( <link rel="stylesheet" type="text/css" href={'/_next/static/css/styles.chunk.css?v=' + Date.now()} /> )} </Head> <div className="main-content">{children}</div> </div> ); } }
How ur layout will work in _app.js For example
<Container>
<Layout>
<Component>
</Layout>
</Container>