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 server renderToHTML does not work with next 13.4 #54977

Open
1 task done
thilohaas opened this issue Sep 4, 2023 · 28 comments
Open
1 task done

Custom server renderToHTML does not work with next 13.4 #54977

thilohaas opened this issue Sep 4, 2023 · 28 comments
Labels
bug Issue was opened via the bug report template.

Comments

@thilohaas
Copy link

thilohaas commented Sep 4, 2023

Link to the code that reproduces this issue or a replay of the bug

https://github.com/thilohaas/nextjs-custom-server/tree/main
https://codesandbox.io/p/github/thilohaas/nextjs-custom-server/main

To Reproduce

  1. Start a new nextjs project with npx create-next-app@latest
  2. Add a server.js
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');

const dev = process.env.NODE_ENV !== "production";
const hostname = 'localhost';
const port = process.env.PORT || 3001;
// when using middleware `hostname` and `port` must be provided below
const app = next({ hostname, port, dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer(async (req, res) => {
    try {
      const parsedUrl = parse(req.url, true);
      const { pathname, query } = parsedUrl;

      if (pathname.startsWith('/_next') || pathname.startsWith('/__next')) {
        await handle(req, res, parsedUrl);
      } else {
        const html = await app.renderToHTML(req, res, pathname, query);
        res.end(html);
      }
    } catch (err) {
      console.error('Error occurred handling', req.url, err);
      res.statusCode = 500;
      res.end('internal server error');
    }
  }).listen(port, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://${hostname}:${port}`);
  });
});
  1. Change the package.json dev script to: "dev": "node server.js",
  2. run npm run dev
  3. open http://localhost:3001

Current vs. Expected behavior

I expect it to display the welcome page but instead i get an error from the next server:

Error: Invariant ensurePage called outside render worker

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
    Binaries:
      Node: 16.17.0
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: 7.1.0
    Relevant Packages:
      next: 13.4.20-canary.16
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.3
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

No response

@thilohaas thilohaas added the bug Issue was opened via the bug report template. label Sep 4, 2023
@sreimer15
Copy link

I'm seeing this error as well, when using next-dev-https

@KODerFunk
Copy link

For me this problem is also related to this one: #53466
Obstacles have appeared in large projects with their own infrastructure (Kube).
These problems are associated with the use of Custom-Server, which is a great need for additional collection of metrics, custom logging, control of response headers and status, etc. I also need the ability to run in one thread so that the AsyncLocalStorage context is saved and other global mechanics work.

On versions higher than 13.4.12 (I confirm, the problem appears with 13.4.13-canary.0) it became impossible to create a custom-server. Now when calling methods, and even render, I get errors when calling server methods: Invariant ... called outside render worker.

The Next server is initializing, but it seems to be isolated from my Fastify server, or the new code is broken.
@ijjk It seems to me that you added these changes that affected an important part of the community.

@JaviCodx
Copy link

We are having the same issue with a custom Express server

@salman-mazhar
Copy link

We are facing the same issue when using renderToHTML with a custom http server, is there a temporary work around?

@KODerFunk
Copy link

@salman-mazhar We are waiting for @ijjk.

@linhucontorion
Copy link

@jiebaibai please use English to report your issue, so others can understand, thanks

@jiebaibai
Copy link

@jiebaibai please use English to report your issue, so others can understand, thanks

@ijjk 13.5.5 also has this problem, after customizing server.js, app.renderToHTML exception,catch error is:
TypeError: Cannot read properties of undefined (reading 'ensurePage')
image

@QianKuang8
Copy link

QianKuang8 commented Oct 23, 2023

Also face a similar issue in Custom Server. Our AssetPrefix is partially working.

@saurabhgupta050890
Copy link

Same issue with Next 14.0.3 with custom server. We are having a hapi server

@gustavoalvesm
Copy link

Same problem here with custom server + renderToHTML + hydrate components 😞 any temporary solution?

image

@ESCxjl
Copy link

ESCxjl commented Dec 28, 2023

We are facing the same issue when using renderToHTML with a custom http server, anyone can help me ?

@chngjunjie
Copy link

chngjunjie commented Jan 2, 2024

Any update for this issue ? we are facing the same issue with hapi js. renderToHTML issue , version 13.4

The isRenderWorker is always undefined in next-dev-server. So it will work if we turn off the development mode, build the dist folder and run it without next server.

@SouthLink
Copy link
Contributor

Same issue with Next 14.0.4 with custom server.

rallu added a commit to klevultd/frontend-sdk that referenced this issue Jan 18, 2024
@emreozkandev
Copy link

emreozkandev commented Jan 23, 2024

do not use custom next serve. manage routers via next.config.js

@redguard1
Copy link

I have a same problem with next 14.1.0 with custom server. Any update for this issue ?

@mildfuzz
Copy link

mildfuzz commented Feb 6, 2024

Also seeing this error

@emreozkandev
Copy link

I solved my problem by managing the routes in next.config.js

@mildfuzz
Copy link

mildfuzz commented Feb 6, 2024

@duzgunemreozkan yeah, you mentioned.

Maybe you might elaborate on how you are able to replicate the functionality of Express in the next.config.js, that would be helpful

@azraelgarden
Copy link

Same error here (Next 14. while trying to work with Hydrate and Stencil (Web Components).
TypeError: Cannot read properties of undefined (reading 'ensurePage')

@mildfuzz
Copy link

mildfuzz commented Feb 16, 2024 via email

@vaishnavi-jadhav-amla
Copy link

Iam facing the same issue. Iam not able to use renderToHTML on custom server. Iam using next 13.4. Any workaround for this issue. I also tried on the latest version but it still not worked.

@rtrys
Copy link

rtrys commented Apr 8, 2024

I'm seeing the same error with custom server (I'm using Hapi)

@ChangWei-Chang
Copy link

Any update or workaround on this ? We are planning to upgrade to next 14.2.3 from 12 and see this issue.

@Rylab
Copy link
Contributor

Rylab commented Jun 15, 2024

We ran into this problem too, it's with renderToHtml only in dev mode. We got around it by calling app.render in dev mode instead.

@rv-dswanson
Copy link

rv-dswanson commented Jun 26, 2024

We ran into this problem too, it's with renderToHtml only in dev mode. We got around it by calling app.render in dev mode instead.

This worked for my team also, going from 13.2.3 to 14.2.4

Nvm saw errors in server

@mayerraphael
Copy link

mayerraphael commented Jul 1, 2024

A workaround is to directly access the internal Dev/Node server inside the app object.

const appHtml = await (app as any).renderServer.server.renderToHTML(req, res, pathname!, query);

This works with 14.2.4.

Problem is the getServer() method. Which is called on the app ( CustomServer ) object, but not on the already created DevNextServer. Therefor another new server is created with an undefined bundlerService. app (which is a CustomServer), has no serverPromise.

app.renderServer or app.renderServer.server are the correct objects.
image

@lzkui2013
Copy link

lzkui2013 commented Jul 9, 2024

Aslo face the same thing,when use customServer to get the page html to cache in redis。

const html = await app.renderToHTML(req, res, pathname, query);

Alway error: Invariant: dynamic responses cannot be unchunked. This is a bug in Next.js

Nextjs version: 14.2.3

@6ixB

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests