next.js icon indicating copy to clipboard operation
next.js copied to clipboard

next export does't work with custom webpack loaders

Open felquis opened this issue 8 years ago • 17 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

To work properly and frictionless with all kinds of webpack loaders.

Current Behavior

next dev seems to work with custom loaders, but it is very confusing, like what is the version of babel beeing used, you go to tutoriais on the internet and it uses options from older module versions next build and next export doest integrate well with custom loaders, it thorwn a error.

Steps to Reproduce (for bugs)

Add the loaders to next.config.js

tip: find and run an example that is easy to hack

config.module.rules.push({ test: /\.(jpg|png))/, use: [{ loader: 'file-loader' }, { loader: 'url-loader', options: { limit: 1 } }]})
npm install -S file-loader url-loader

Context

From a fresh new create-next-app project, add a image to /static, import the image at /page/index.js, and use it as a url string <img src={myImage} />, then add the custom loader, and try to

1 - use development server 2 - build and use static version to production (npm i -g serve can help) 3 - build and use production server

Your Environment

Tech Version
next 4.2
node 9.4
OS Windows 10

felquis avatar Jan 17 '18 17:01 felquis

We don't recommend the usage of custom loaders for the moment. https://github.com/zeit/next.js#customizing-webpack-config

Warning: Adding loaders to support new file types (css, less, svg, etc.) is not recommended because only the client code gets bundled via webpack and thus it won't work on the initial server rendering. Babel plugins are a good alternative because they're applied consistently between server/client rendering (e.g. babel-plugin-inline-react-svg).

sergiodxa avatar Jan 17 '18 20:01 sergiodxa

Next.js v5 is going to support this.

timneutkens avatar Jan 17 '18 23:01 timneutkens

we are on v5 now, is it supported already? I'm still getting exception from ssr when using url-loader for svg - it's rendered ok on the client, when rendered on server - returns an error 'unexpected token <'

vic08 avatar Feb 09 '18 09:02 vic08

@timneutkens This is my setup for png and jpg images:

...
conf.module.rules.push(
{
  test: /\.(jpe?g|png)$/,
  use: [
      {
        loader: "url-loader",
        options: {
          limit: 8192
        }
      }
    ]
  }
);
...

And when I import an image in one of my pages, I will get the following error:

GET http://localhost:3000/d46ff27102320adcf5f14c437a32b78c.jpg 404 (Not Found)
main.js:4530 Warning: Prop `src` did not match. Server: "d46ff27102320adcf5f14c437a32b78c.jpg" Client: "/_next/webpack/d46ff27102320adcf5f14c437a32b78c.jpg"

P.S. I'm using Next v5

arefaslani avatar Feb 13 '18 10:02 arefaslani

Looks like you'll have to configure fallback to a custom file-loader configuration https://github.com/webpack-contrib/url-loader#options

And then set https://github.com/webpack-contrib/file-loader#options

publicPath: '/_next/static/images' and outputPath: 'static/images'

Something like this should work (haven't tested it):

conf.module.rules.push(
{
  test: /\.(jpe?g|png)$/,
  use: [
      {
        loader: "url-loader",
        options: {
          limit: 8192,
		  fallback: {
             loader: 'file-loader',
             options: { publicPath: '/_next/static/images', outputPath: 'static/images' }
          }
        }
      }
    ]
  }
);

timneutkens avatar Feb 13 '18 11:02 timneutkens

Also, feel free to create a plugin for this on your own github (not next-plugins), I'm sure a lot of people will like it 👍 🙏

timneutkens avatar Feb 13 '18 11:02 timneutkens

This worked for me:

conf.module.rules.push(
{
  test: /\.(jpe?g|png)$/,
  use: [
      {
        test: /\.(jpe?g|png|svg|gif)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 8192,
              fallback: "file-loader",
              publicPath: "/_next/",
              outputPath: "static/images/",
              name: "[name]-[hash].[ext]"
            }
          }
        ]
      }
    ]
  }
);

Also, feel free to create a plugin for this on your own github (not next-plugins), I'm sure a lot of people will like it 👍 🙏

Sure I will if I can do it 👍

arefaslani avatar Feb 13 '18 12:02 arefaslani

Feel free to @arefaslani 🙏

timneutkens avatar Feb 13 '18 13:02 timneutkens

So was it a problem with fallback for url-loader? I thought 'file-loader' is a default value for fallback

vic08 avatar Feb 13 '18 15:02 vic08

publicPath: "/_next/",
outputPath: "static/images/",

otherwise it ends up in a non-served path.

timneutkens avatar Feb 13 '18 16:02 timneutkens

I'm very happy with next 5 the plugin approach will help us a lot, thanks @timhuff https://github.com/zeit/next.js/issues/3586#issuecomment-365230746

I'm already using it here https://github.com/felquis/static-website

felquis avatar Feb 13 '18 18:02 felquis

@felquis I'm guessing you meant @timneutkens?

timhuff avatar Feb 13 '18 21:02 timhuff

@timneutkens I'm thinking I accidentally got mentioned because I'm now a contributor for adding a single line to a README on a sample project. So we're both put first in the mentions and "timh" comes before "timn". Do you know if there's a way to delist myself as a contributor so we can make everyone's life a little easier when it comes to mentions?

timhuff avatar Feb 13 '18 21:02 timhuff

@timhuff I don't think we can on our side, you might be able to send a support request to github 🤔

timneutkens avatar Feb 13 '18 22:02 timneutkens

Alright. Support request sent. It sounds silly but I can see this being a pain for a lot of commenters. If I had made an actual contribution, it'd be different but I almost wish I had told a friend to update the README. It seems that the real solution here is to introduce code to the mentions resolver that puts priority on mentions of the owner.

timhuff avatar Feb 13 '18 22:02 timhuff

👍 Sorry for the inconvenience 🙏

timneutkens avatar Feb 13 '18 22:02 timneutkens

Hah. I think this is on me. But I guess you did accept the pull request. For shame.

timhuff avatar Feb 13 '18 22:02 timhuff