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

windows serverMiddleware HMR edge case #7097

Closed
usb248 opened this issue Mar 17, 2020 · 9 comments
Closed

windows serverMiddleware HMR edge case #7097

usb248 opened this issue Mar 17, 2020 · 9 comments

Comments

@usb248
Copy link

usb248 commented Mar 17, 2020

Version

v2.12.0

Reproduction link

https://jsbin.com/

Steps to reproduce

Modify a servermiddleware file

What is expected ?

No script crash

What is actually happening?

D:\wamp64\www...\node_modules@nuxt\builder\dist\builder.js:6160
for (const entry of dep2Entry[fileName]) {
^

TypeError: dep2Entry[fileName] is not iterable

@ghost ghost added the cmty:bug-report label Mar 17, 2020
@pi0
Copy link
Member

pi0 commented Mar 17, 2020

Hi, @usb248 thanks for reporting the issue. But without repro, it is impossible to debug. Would you please share nuxt config or more details?

@usb248
Copy link
Author

usb248 commented Mar 17, 2020

This evening no, tomorrow. But i can say now that i have:

serverMiddleware: [
    bodyParser.json(),
    bodyParser.text(),
    '~/servermiddleware/cookie-session',
    '~/servermiddleware/csrf',
    '~/api'
  ],

An index.js and others directories inside api directory. @pi0 maybe a case which is not managed by HMR for server middleware (or maybe bodyParser) ?

@pi0
Copy link
Member

pi0 commented Mar 18, 2020

Hi, @usb248 I tried using provided parts but could not reproduce yet. maybe because something special is inside server middleware or API?

I added a quick fix commit (4b34941) to ensuring entry is in the graph but seems the root cause is an entry point is not being tracked.

If you had time, would you please to debug this? Either if you can give me (privately) access to project with this problem or yourself can start by editing D:\wamp64\www...\node_modules@nuxt\builder\dist\builder.js, search for debounce((event, fileName) and log what file is missing from graph like this:

      debounce((event, fileName) => {
+        if (!dep2Entry[fileName]) {
+          console.warn('HMR Entrypoint is missing but reloaded: ' + fileName)
+          return
+        }
        for (const entry of dep2Entry[fileName]) {

@pi0 pi0 changed the title Watcher failed new version API HMR fails with 2.12.0 in some conditions Mar 18, 2020
@usb248
Copy link
Author

usb248 commented Mar 18, 2020

@pi0 yes. index.js in api folder seems to cause the problem...
image

Content of this file :

// Create express router
import express from 'express'
const router = express.Router()

const cors = require('cors')
const urlP = require('url')
const Csrf = require('csrf')
const tokens = new Csrf()
const Multer = require('multer')

var app = express()

router.use(cors({
  origin: process.env.BASE_URL,
  optionsSuccessStatus: 200
}))

router.use((req, res, next) => {

  Object.setPrototypeOf(req, app.request)
  Object.setPrototypeOf(res, app.response)
  req.res = res
  res.req = req

  req.query = urlP.parse(req.url, true).query

  res.removeHeader('X-Powered-By')

  const safeMethods = /GET|HEAD|OPTIONS|TRACE/i

  if (safeMethods.test(req.method)) return next()

  /* Protection CSRF */
  const verify = (req) => {
    return tokens.verify(req.session.csrfSecret || '', req.headers['x-csrf-token'] || '')
  }

  if (verify(req)) return next()

  const error = new Error('Token invalide.')
  error.status = 403
  return next(error)

})

app.use(router)

import routes from './routes'
app.use(routes)

if(process.env.NODE_ENV === 'production') app.set('trust proxy', true)

app.use((err, req, res, next) => {
  if (err) {
    if (err instanceof Multer.MulterError) {

      const errorMessages = {
        'LIMIT_PART_COUNT': 'Trop de champs.',
        'LIMIT_FILE_SIZE': 'Fichier trop volumineux.',
        'LIMIT_FILE_COUNT': 'Trop de fichiers.',
        'LIMIT_FIELD_KEY': 'Nom du fichier trop long.',
        'LIMIT_FIELD_VALUE': 'Valeur du champ trop longue.',
        'LIMIT_FIELD_COUNT': 'Trop de champs de type non fichier.',
        'LIMIT_UNEXPECTED_FILE': 'Champ inattendu.'
      }

      if(errorMessages[err.code]) err.message = errorMessages[err.code]
    }
    return res.status(err.status || 422).json({ msg: (err.hasOwnProperty('message')) ? err.message : err.toString() })
  }
  next()
})

// Export the server middleware
export default {
  path: '/api',
  handler: app
}

@pi0
Copy link
Member

pi0 commented Mar 18, 2020

@usb248 My most guess is that it is a windows issue. Will try on windows machine :)

@usb248
Copy link
Author

usb248 commented Mar 18, 2020

let me know if this is the case.
Thanks :)

@MLDMoritz
Copy link

Probably windows screws up filePaths. For a temporary solutions, may install linux subsystem on windows (Will speed up dev too ;-))

This was referenced Mar 25, 2020
@pi0
Copy link
Member

pi0 commented Mar 25, 2020

Update:

This issue is happening for windows users that have a serverMiddleware registered with implicit index.js

@pi0 pi0 changed the title API HMR fails with 2.12.0 in some conditions windows serverMiddleware HMR edge case Mar 25, 2020
@pi0
Copy link
Member

pi0 commented Mar 31, 2020

Hi again. It should be fixed on 2.12.2 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants