question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`transformIndexHtml` hook gets the wrong html filepath when running Multi-Page

See original GitHub issue

Describe the bug

transformIndexHtml hook gets the wrong html filepath when running Multi-Page.

Reproduction

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolvePath } from './utils'

export default defineConfig({
  plugins: [
    vue(),
  ],

  build: {
    rollupOptions: {
      input: {
        main: resolvePath('index.html'),
        landingPage: resolvePath('landing-page/index.html'),
      },

    },
  },
})

Project root directory structure:

  • index.html
  • landing-page
    • index.html

in the transformIndexHtml hook:

async transformIndexHtml(html, { filename }) { ... }

Go to /landing-page/ filename will be <root>/landing-page/index.html witch is correct.

but, if go to /landing-page/foo/ filename will be <root>/index.html which is the the wrong filename.

it really should be <root>/landing-page/index.html when the url is subpath of /landing-page/.

Sure I can fix this by add an additional middleware to the ViteDevServer, but it really should detect if it is a Multi-Page, if it is, add the routes for it automatically.

Reproduction

  1. clone https://github.com/ssr-glue/vite-plugin
  2. run yarn
  3. remove comment https://github.com/ssr-glue/vite-plugin/blob/755371d940fbea6365a38cab0f9c03e868f0db91/packages/playground/vue/vite.config.ts#L18-L27
  4. add console.log to log the ctx.filename, https://github.com/ssr-glue/vite-plugin/blob/755371d940fbea6365a38cab0f9c03e868f0db91/packages/vite-plugin/src/plugin.ts#L67-L78 then run yarn build
  5. goto packages/playground/vue run yarn dev
  6. goto http://localhost/landing-page/about to see the log

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
poyohocommented, Mar 12, 2022

you can use middleware in vite dev server to rewrite url what you want.

const { defineConfig } = require('vite')
module.exports = defineConfig({
  plugins: [
    {
      name: 'rewrite-middleware',
      configureServer(serve) {
        serve.middlewares.use((req, res, next) => {
          if (req.url.startsWith('/nested/')) {
            req.url = '/nested/'
          }
          next()
        })
      }
    }
  ]
})

so /nested/**/* will go to <root>/nested/index.html

1reaction
bluwycommented, Mar 6, 2022

I second @anncwb. Vite currently performs a simple fallback when the route index.html is not found. Resolving subpaths to their own index.html (as shown in the issue description), feels like a special routing behaviour that should be implemented in userland via server middlewares. If Vite happens to support this today, it would be a breaking change too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plugin API - Vite
Dedicated hook for transforming HTML entry point files such as index.html . The hook receives the current HTML string and a transform context....
Read more >
Why does Javascript/Vite.js multi-page app fail on build?
When add home.html to my config file, the build breaks. I get an error saying: [vite:build-html] EISDIR: illegal operation on a directory, ...
Read more >
React Redux Hooks Explained - Morioh
Hi guys, In this video I explained about React Redux Hooks.
Read more >
Recommended - SlideShare
Dedicated hook for transforming HTML entry point files such as ... For production: after running vite build , a manifest.json file will.
Read more >
Vue-CLI migration Vite2 - Programmer Sought
And Vite directly uses the HTML file, which will resolve the Script tag in the HTML to find ... transformIndexHtml Hook, receive the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found