Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Not finding _next/static/ #77

Closed
zscaiosi opened this issue Feb 27, 2018 · 6 comments
Closed

Not finding _next/static/ #77

zscaiosi opened this issue Feb 27, 2018 · 6 comments

Comments

@zscaiosi
Copy link
Contributor

zscaiosi commented Feb 27, 2018

Hi, I am using next-css with next-typescript, but when I import inside my _document.tsx the CSS files, which are all inside _next/static/, the styles are not applied to the pages of the app.

My project/pages/_document.tsx tag has:

<link rel="stylesheet" href="../_next/static/header.css" />
<link rel="stylesheet" href="../_next/static/index.css" />

My next.config.js is:
`
const withTypescript = require('@zeit/next-typescript');

const withCSS = require('@zeit/next-css');

module.exports = withCSS(withTypescript({
webpack(config, options) {
return config
},
typescriptLoaderOptions: {
transpileOnly: false
}
}));
`

My _next/ and pages/ folders are ate the same leve in the Project's root folder.

The styles are not being applied anywhere and I can't figure out why.

@timneutkens
Copy link
Member

Using next export? try upgrading to next@canary

@zscaiosi
Copy link
Contributor Author

Hi @timneutkens, I am not using next export, I am using next-css exactly as described in the README.md

@kachkaev
Copy link
Contributor

kachkaev commented Mar 5, 2018

I'm also unable to make @zeit/next-typescript and @zeit/next-css work together.

next.config.js:

const withTypescript = require("@zeit/next-typescript");
const withCss = require("@zeit/next-css");
module.exports = withTypescript(withCss());

page/_document.tsx:

import Document, { Head, Main, NextScript } from "next/document";
import { ServerStyleSheet } from "styled-components";

import styles from "../styles/index.css";

export default class MyDocument extends Document {
  public static getInitialProps({ renderPage }) {
    const sheet = new ServerStyleSheet();
    const page = renderPage((App) => (props) =>
      sheet.collectStyles(<App {...props} />),
    );
    const styleTags = sheet.getStyleElement();
    return { ...page, styleTags };
  }

  public render() {
    return (
      <html>
        <Head>
          <title>Test Page</title>
          <link rel="stylesheet" href="/_next/static/style.css" />
          {this.props.styleTags}
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}

Requests to http://localhost:3000/_next/static/style.css return 404 and there is no .next/static folder during yarn dev or after yarn build.

    "@zeit/next-typescript": "^0.0.10",
    "@zeit/next-css": "^0.1.3",
    "next": "^5.0.1-canary.10",

I'm using a custom ts server to enable i18next, and there I've got:

// ...
app.prepare().then(() => {
const server = express();

// enable middleware for i18next
server.use(i18nextMiddleware.handle(i18nInstance));

// serve locales for client
server.use("/locales", express.static(join(__dirname, "../locales")));

// missing keys
server.post(
    "/locales/add/:lng/:ns",
    i18nextMiddleware.missingKeyHandler(i18nInstance),
);

// use next.js
server.get("*", (req, res) => {
    // const parsedUrl = parse(req.url, true);
    (req as any).graphqlUri = env.GRAPHQL_URI;
    // req.i18n = i18nInstance;
    handle(req, res);
});

server.listen(env.PORT, (err) => {
    if (err) {
    throw err;
    }
    console.log(`> Ready on port ${env.PORT}`);
});
// ...

What is also interesting is that if I replace import styles from "../styles/index.css"; with import "../styles/index.css"; in _document.tsx (which seems more correct), yarn dev crashes:

Error: Module build failed: TypeError: /path/to/project/pages/_document.tsx: Cannot read property 'local' of undefined
    at PluginPass.exit (/path/to/project/node_modules/babel-plugin-inline-import/build/index.js:29:48)
    at newFn (/path/to/project/node_modules/babel-traverse/lib/visitors.js:276:21)
    at NodePath._call (/path/to/project/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/path/to/project/node_modules/babel-traverse/lib/path/context.js:48:17)
    at NodePath.visit (/path/to/project/node_modules/babel-traverse/lib/path/context.js:117:8)
    at TraversalContext.visitQueue (/path/to/project/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitMultiple (/path/to/project/node_modules/babel-traverse/lib/context.js:103:17)
    at TraversalContext.visit (/path/to/project/node_modules/babel-traverse/lib/context.js:190:19)
    at Function.traverse.node (/path/to/project/node_modules/babel-traverse/lib/index.js:114:17)
    ...

Moving import from pages/_document.ts to any other ts file does not change anything. Manually creating .next/static/style.css heals from 404 response and serves that dummy CSS correctly (which suggests that the custom server is OK).

@timneutkens what can be wrong in this setup?

@kachkaev
Copy link
Contributor

kachkaev commented Mar 5, 2018

UPD: I finaly made styles partially working in my TypeScript + CSS project. The trick was to use require instead of import and keep the references to styles away from pages/_document.tsx.

I moved styles to lib/hocs/page.ts, which is a higher-order component that wraps every page.

import { compose } from "recompose";

import "../pageEvents";
import withData from "./withData";
import withI18next from "./withI18next";

// tslint:disable:no-var-requires
require("normalize.css");
require("../../styles/base.css");
require("../../styles/nprogress.css");

export default (i18nextNamespaces = ["common"]) =>
  compose(withData, withI18next(i18nextNamespaces));

Unfortunately, I haven't been able to replace three requires with require("../../styles/index.css"); that looks like this:

@import "normalize.css";
@import "./base.css";
@import "./nprogress.css";

When I do so I get:

 Failed to compile with 1 errors                                                                                                                         6:53:13 PM

This dependency was not found:

* -!../node_modules/css-loader/index.js??ref--4-2!./normalize.css in ./styles/index.css

To install it, you can run: npm install --save -!../node_modules/css-loader/index.js??ref--4-2!./normalize.css

/path/to/project/node_modules/next/dist/server/on-demand-entry-handler.js:141
      }, []).map(function (c) {
             ^
TypeError: Cannot read property '1' of null
    at /path/to/project/node_modules/next/dist/server/on-demand-entry-handler.js:142:61
    at Array.map (<anonymous>)
    at Compiler.<anonymous> (/path/to/project/node_modules/next/dist/server/on-demand-entry-handler.js:141:14)
    at Compiler.applyPlugins (/path/to/project/node_modules/tapable/lib/Tapable.js:61:14)
    at Watching._done (/path/to/project/node_modules/webpack/lib/Compiler.js:105:17)
    at onCompiled (/path/to/project/node_modules/webpack/lib/Compiler.js:55:18)
    at applyPluginsAsync.err (/path/to/project/node_modules/webpack/lib/Compiler.js:510:14)
    at next (/path/to/project/node_modules/tapable/lib/Tapable.js:202:11)
    at Compiler.<anonymous> (/path/to/project/node_modules/webpack/lib/CachePlugin.js:78:5)
    at Compiler.applyPluginsAsyncSeries (/path/to/project/node_modules/tapable/lib/Tapable.js:206:13)
    at compilation.seal.err (/path/to/project/node_modules/webpack/lib/Compiler.js:507:11)
    at Compilation.applyPluginsAsyncSeries (/path/to/project/node_modules/tapable/lib/Tapable.js:195:46)
    at self.applyPluginsAsync.err (/path/to/project/node_modules/webpack/lib/Compilation.js:680:19)
    at Compilation.applyPluginsAsyncSeries (/path/to/project/node_modules/tapable/lib/Tapable.js:195:46)
    at self.applyPluginsAsync.err (/path/to/project/node_modules/webpack/lib/Compilation.js:671:11)
    at Compilation.applyPluginsAsyncSeries (/path/to/project/node_modules/tapable/lib/Tapable.js:195:46)

Any thoughts why css @imports are not being resolved?

@kachkaev
Copy link
Contributor

kachkaev commented Mar 5, 2018

Me again 😅 !

Here is what worked in /path/to/project/styles/index.css

@import "../node_modules/normalize.css/normalize.css";
@import "./base.css";
@import "./nprogress.css";

🎉

To summarize, here are the remaining issues in my @zeit/next-typescript and @zeit/next-css project:

  • use of require instead of import inside ts files, otherwise things crash
  • inability to reference css files inside pages/_document.ts despite that this is probably the most natural place for them
  • potential caching problems in production due to how to clean browser css cache #11

@pencilcheck
Copy link

Have the same issue, using 5.1.0, 404 on /_next/static/style.css. Also followed the instructions

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants