Razzle: Server and client content do not match in TS app

Created on 20 Jun 2018  ยท  8Comments  ยท  Source: jaredpalmer/razzle

Using a fresh copy of the with-typescript example, I've run into an error Text content did not match. Server: "Welcome to Razzles" Client: "Welcome to Razzle" after I made a quick edit in Home.tsx. While HMR works fine on the client and the content changes as expected, the error is thrown after I reload the page. I'm not seeing this issue after creating a new Razzle app using create-razzle-app with JS, though, so it leads me to think something may not be right with the build process using TS.

razzle: 2.2.0
razzle-plugin-typescript: 2.2.0
typescript: 2.9.2

stale

Most helpful comment

According to ts-loader's doc:

https://github.com/TypeStrong/ts-loader#hot-module-replacement

Inside your HMR acceptance callback function, you must re-require the module that was replaced.

So I made some changes to src/index.ts and it finally works:

import express from 'express'
import app from './server'

const port = process.env.PORT || 3000

// 1. a dynamic middleware
let middleware = app

express()
  .use((req, res, next) => {
    // 2. use that middleware
    middleware(req, res, next)
  })
  .listen(port, (err: Error) => {
    if (err) {
      console.error(err)
      return
    }
    console.log(`> Started on port ${port}`)
  })

if (module.hot) {
  module.hot.accept('./server', () => {
    // 3. re-require the module
    const nextApp: any = require('./server').default
    // 4. and assign it to the middleware
    middleware = nextApp

    console.log('๐Ÿ”  HMR Reloading `./server`...')
  })
  console.info('โœ…  Server-side HMR Enabled!')
}

All 8 comments

Same here ๐Ÿ‘‹. HMR server logs looks fine like...

๐Ÿ”  HMR Reloading `./server`...
[HMR] Updated modules:
[HMR]  - ./src/server.tsx
[HMR] Update applied.

... but I have to exit yarn start and run it again to see my changes on server. Client HMR works fine. Default Razzle app works nice with server reloads but TS version is broken.

razzle: 2.2.0
razzle-plugin-typescript: 2.2.0
typescript: 2.8.3
node: 8.11.3

+1

According to ts-loader's doc:

https://github.com/TypeStrong/ts-loader#hot-module-replacement

Inside your HMR acceptance callback function, you must re-require the module that was replaced.

So I made some changes to src/index.ts and it finally works:

import express from 'express'
import app from './server'

const port = process.env.PORT || 3000

// 1. a dynamic middleware
let middleware = app

express()
  .use((req, res, next) => {
    // 2. use that middleware
    middleware(req, res, next)
  })
  .listen(port, (err: Error) => {
    if (err) {
      console.error(err)
      return
    }
    console.log(`> Started on port ${port}`)
  })

if (module.hot) {
  module.hot.accept('./server', () => {
    // 3. re-require the module
    const nextApp: any = require('./server').default
    // 4. and assign it to the middleware
    middleware = nextApp

    console.log('๐Ÿ”  HMR Reloading `./server`...')
  })
  console.info('โœ…  Server-side HMR Enabled!')
}

Weirddds. I just have it has index.js in our apps.

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

@crosscompile did we ever fix this internally?

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

corydeppen picture corydeppen  ยท  3Comments

Jayphen picture Jayphen  ยท  4Comments

krazyjakee picture krazyjakee  ยท  3Comments

GouthamKD picture GouthamKD  ยท  3Comments

panbanda picture panbanda  ยท  5Comments