next-plugins icon indicating copy to clipboard operation
next-plugins copied to clipboard

Cant change page with 'next/link' & 'next-css'

Open standy opened this issue 8 years ago • 221 comments

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

standy avatar Sep 24 '18 11:09 standy

I have the exact same issue, on the exact same versions as well.

Confirmed work around: make every page import some css.

Dynogic avatar Sep 26 '18 15:09 Dynogic

Was trying hard to determine what was causing that. Thanks for reporting @Dynogic and @standy

virzak avatar Sep 26 '18 15:09 virzak

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.

Enalmada avatar Oct 03 '18 00:10 Enalmada

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 avatar Oct 03 '18 18:10 kylemh

@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

nisargrthakkar avatar Oct 15 '18 13:10 nisargrthakkar

It's a strategy some have professed, but it doesn't resolve anything for me - just like you.

kylemh avatar Oct 15 '18 18:10 kylemh

Same problem

popuguytheparrot avatar Oct 17 '18 15:10 popuguytheparrot

@kylemh / others could you try @zeit/next-css@canary? It holds #315

timneutkens avatar Oct 22 '18 21:10 timneutkens

I would fuckin' love to ❤️

kylemh avatar Oct 22 '18 22:10 kylemh

@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.

aserranom avatar Oct 22 '18 22:10 aserranom

^ this precisely.

It doesn't look like I'm getting more CSS files per route - just on initial load.

SSR gets all styles properly.

kylemh avatar Oct 22 '18 22:10 kylemh

Same issue with @zeit/next-css@canary not get css load on route change

nisargrthakkar avatar Oct 23 '18 04:10 nisargrthakkar

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?

kylemh avatar Oct 23 '18 05:10 kylemh

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.

plag avatar Oct 23 '18 05:10 plag

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

plag avatar Oct 23 '18 07:10 plag

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

popuguytheparrot avatar Oct 23 '18 07:10 popuguytheparrot

@plag thanks so much for sharing that! 😄

stian-midlertidig avatar Oct 23 '18 08:10 stian-midlertidig

@plag I've updated your comment to use Router.events instead: https://github.com/zeit/next.js#router-events

timneutkens avatar Oct 23 '18 10:10 timneutkens

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>
    );
  }
}

zhanglin-doudou avatar Oct 24 '18 03:10 zhanglin-doudou

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>
    );
  }
}

How ur layout will work in _app.js For example

<Container>
  <Layout>
    <Component>
  </Layout>
</Container>

popuguytheparrot avatar Oct 24 '18 08:10 popuguytheparrot