fastify / fastify-vite

Fastify plugin for Vite integration.
MIT License
861 stars 72 forks source link

Quickstart steps lead to infinite reload loop if HTTPS enabled #74

Closed isaacs closed 2 years ago

isaacs commented 2 years ago

Prerequisites

Fastify version

3.28.0

Plugin version

2.3.1

Node.js version

17.6.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

21.4.0

Description

Following steps at: https://fastify-vite.dev/quickstart/vue.html result in an infinite reload loop. No idea why.

Steps to Reproduce

  1. Install deps:
npm i fastify fastify-vite fastify-vite-vue --save
  1. app.js
import Fastify from 'fastify'
import FastifyVite from 'fastify-vite'
import renderer from 'fastify-vite-vue'

import {readFileSync} from 'node:fs'
const https = {
  cert: readFileSync('tier-dev-tailscale.pem'),
  key: readFileSync('tier-dev-tailscale-key.pem'),
}

const root = import.meta.url
const app = Fastify({
  logger: true,
  https,
})

await app.register(FastifyVite, { root, renderer })
await app.vite.commands()
await app.listen(+process.env.PORT || 443, '0.0.0.0')
  1. views/index.vue
<template>
  <h1>Hello World</h1>
</template>

<script>
export const path = '/'
</script>
  1. package.json file:
{
  "name": "@tier.run/tierweb",
  "version": "0.0.0",
  "type": "module",
  "main": "app.js",
  "dependencies": {
    "fastify": "^3.28.0",
    "fastify-vite": "^2.3.1",
    "fastify-vite-vue": "^2.3.1",
    "pino-pretty": "^7.6.0"
  }
}
  1. Run node app.js
  2. Open page in browser, results in infinite flashing re-load.

Resulting logs: https://gist.github.com/b3934ff57a86a5cb6b51616ddce2b4c3

Expected Behavior

Should load content once, not infinitely many times as fast as possible.

galvez commented 2 years ago

Thanks for the report, I'm looking into this shortly.

galvez commented 2 years ago

Does it still happen in production mode? node NODE_ENV=production app.js

I'm afraid Vite's development server might not work in HTTPS mode.

isaacs commented 2 years ago

@galvez That results in this error:

$ NODE_ENV=production node app.js
Error: Missing production client/index.html — did you build first?
    at recalcDist (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/fastify-vite/options.js:91:15)
    at processOptions (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/fastify-vite/options.js:100:3)
    at fastifyVite (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/fastify-vite/index.js:32:21)
    at Plugin.exec (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/avvio/plugin.js:132:19)
    at Boot.loadPlugin (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/avvio/plugin.js:274:10)
galvez commented 2 years ago

Yes, because to run in production mode first you have to build the app bundle: node app.js build.

In development mode the bundle is generated automatically for development.

galvez commented 2 years ago

Sorry, NODE_ENV=production node app.js build.

isaacs commented 2 years ago
$ NODE_ENV=production node app.js build
Error: Missing production client/index.html — did you build first?
    at recalcDist (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/fastify-vite/options.js:91:15)
    at processOptions (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/fastify-vite/options.js:100:3)
    at fastifyVite (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/fastify-vite/index.js:32:21)
    at Plugin.exec (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/avvio/plugin.js:132:19)
    at Boot.loadPlugin (/Users/isaacs/dev/tierdev/tierweb-vue/node_modules/avvio/plugin.js:274:10)
galvez commented 2 years ago

Hey @isaacs please refer to the new README which covers the upcoming v3 release in detail.