nfl / react-helmet

A document head manager for React
MIT License
17.34k stars 660 forks source link

react-helmet mixing fields during renderStatic #667

Open Montana opened 2 years ago

Montana commented 2 years ago

Hey folks,

I'm running server-side render of the React application. I'm using express for this purposes. The whole server-side render code looks like this:

import * as React from "react"
import * as ReactDOMServer from "react-dom/server"
import * as express from "express"
import { StaticRouter } from "react-router-dom"
import walker = require("react-tree-walker")
import { useStaticRendering } from "mobx-react"

import Helmet from "react-helmet"
import Provider from "../src/Provider"
import { StaticRouterContext } from "react-router"
import version = require("../version")

var _template: string = require(`../dist/${version.v()}/index.html`)

interface IRenderResponse {
    statusCode: number,
    template: string,
    redirect?: string
}

I feel like this happens because of react-helmet uses static fields, I suppose. So, if two requests are being handled in parallel, this fields are being changed chaotically. I looked into the thread-safe issue via: https://github.com/openameba/react-safety-helmet. Still no luck.

Cheers, Montana Mendy

edkimmel commented 2 years ago

You should look into rendering in isolated contexts, either with Node's built in VM or a library like VM2 (https://github.com/patriksimek/vm2)

That will let you render multiple requests concurrently without any issues like the above.