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

Custom relative path assetPrefix breaks HMR #18080

Closed
fabb opened this issue Oct 21, 2020 · 7 comments
Closed

Custom relative path assetPrefix breaks HMR #18080

fabb opened this issue Oct 21, 2020 · 7 comments
Milestone

Comments

@fabb
Copy link
Contributor

fabb commented Oct 21, 2020

Bug report

Describe the bug

We have several next.js applications on the same domain, so we want to prefix _next requests. This works both for development and production:

module.exports = {
  assetPrefix: '/docs',
  rewrites() {
    return [
      { source: '/docs/_next/:path*', destination: '/_next/:path*' }
    ]
  }
}

The only thing that does not work are webpack-hmr calls. The calls will also be prefixed, but next.js returns a 404. Seems like the rewrites feature does not consider HMR calls.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Add the configuration mentioned above.
  2. Start local development
  3. See 404s for webpack-hmr calls in the browser network tab

Expected behavior

The rewrites feature should also rewrite HMR requests. In fact, doing the rewrites in a custom express server fixes HMR, so it should be possible.

System information

  • OS: macOS Catalina
  • Browser: Chrome 86
  • Version of Next.js: 9.5.5
  • Version of Node.js: 10.18.4

Additional context

As mentioned here: #5602 (comment)

@adam-foster
Copy link

Hi @fabb I experienced the same issue.

As a work around, you can add the assetPrefix / rewrites config for non development phases only. I.e:

const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')

module.exports = (phase, { defaultConfig }) => {
  if (phase !== PHASE_DEVELOPMENT_SERVER) {
    return {
      assetPrefix: '/docs',
      rewrites() {
        return [
           { source: '/docs/_next/:path*', destination: '/_next/:path*' }
        ]
      }
    }
  }
  return {
    /* development only config options here */
  }
}

@fabb
Copy link
Contributor Author

fabb commented Nov 2, 2020

As a work around, you can add the assetPrefix / rewrites config for non development phases only.

In my case I cannot, because I have several next.js applications that I want to be able to run simultaneously in dev mode on localhost.

@meikidd
Copy link

meikidd commented Jun 7, 2021

I am getting same error in v10.2.3, curious any update on this issue?

@crutch12
Copy link

crutch12 commented Jul 8, 2021

Bump

@crutch12
Copy link

crutch12 commented Jul 8, 2021

Suddenly I found a workaround!

Just add redirects:

async redirects() {
  return [
    // @NOTE: "rewrites" breaks webpack-hmr, so we fix it here. See: https://github.com/vercel/next.js/issues/18080
    {
      source: '/:any*/_next/webpack-hmr:path*',
      destination: '/_next/webpack-hmr:path*',
      permanent: false,
    },
  ];
},

@fabb
Copy link
Contributor Author

fabb commented Nov 5, 2021

fixed in #30632

@fabb fabb closed this as completed Nov 5, 2021
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
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

7 participants