Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build manifest #4119

Merged
merged 8 commits into from
Apr 12, 2018
Merged

Add build manifest #4119

merged 8 commits into from
Apr 12, 2018

Conversation

timneutkens
Copy link
Member

This PR does 2 things:

  1. Makes main.js / manifest.js use chunkhash in production
  2. Add buildManifest as parameter for renderPage which allows users extending the webpack config to use chunkhash for files too. One example of this is that we can modify next-css to use chunkhash in production mode.

@timneutkens
Copy link
Member Author

timneutkens commented Apr 6, 2018

Custom document when adding css will look like:

// _document is only rendered on the server side and not on the client side
// Event handlers like onClick can't be added to this file

// ./pages/_document.js
import Document, { Head, Main, NextScript } from 'next/document'
import flush from 'styled-jsx/server'

export default class MyDocument extends Document {
  render() {
    const {buildManifest, pathname} = this.props
    const {css} = buildManifest
    return (
      <html>
        <Head>
          <style>{`body { margin: 0 } /* custom! */`}</style>
          {css.map((file) => {
            return <link rel="stylesheet" href={`/_next/${file}`} key={file} />
          })}
          {}
        </Head>
        <body className="custom_class">
          {this.props.customValue}
          <Main />
          <NextScript />
        </body>
      </html>
    )
  }
}

@timneutkens
Copy link
Member Author

timneutkens commented Apr 6, 2018

build-manifest.json:

{
  "css": [
    "static/style.css"
  ],
  "main.js": [
    "static/commons/main.js"
  ],
  "manifest.js": [
    "static/commons/manifest.js"
  ]
}

}

if (MATCH_ROUTE_NAME.exec(file)) {
pageFile = file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why store this inside a loop?
Isn't that the previous value could get replaced when we are going to use this?
What's the purpose?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to remove this, we don't need the pages mapping.

return (
const files = buildManifest[filename]

return files.map((file) => (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we are having multiple files?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just how webpack outputs the chunks:

Manifest looks like this:

{
  "css": [
    "static/style.css"
  ],
  "main.js": [
    "static/commons/main.js"
  ],
  "manifest.js": [
    "static/commons/manifest.js"
  ]
}

@barrysteyn
Copy link

@timneutkens
This change is failing for me, however it seems like in the manifest, the webpack compiler is giving an incorrect chunk name.

For example, in my next.config.js, I do this:

const withHashedOutput = (nextConfig = {}) => {
  return Object.assign({}, nextConfig, {
    webpack(config, options) {
      if (!options.defaultLoaders) {
        throw new Error(
          'This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade'
        );
      }

      config.output.filename = "[name][id]";

      if (typeof nextConfig.webpack === 'function') {
        return nextConfig.webpack(config, options);
      }

      return config;
    }
  });
};

const config = withHashedOutput({});

module.exports = config;

So I am just changing the file name by putting it's id. However, the manifest gets the incorrect id number - this is because webpack seems to be returning incorrect information. I am trying this out on 6.0.0-canary.7

Why is this happening? You can easily repro whats happening by using the above - you will see, it won't work.

@timneutkens timneutkens mentioned this pull request May 11, 2018
1 task
@lock lock bot locked as resolved and limited conversation to collaborators May 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants