Skip to content

Vue + Vite + SSR: Referencing public assets with the root path (/myimage.jpg) inside the public directory results in a Rollup error #10082

@truuman

Description

@truuman

Describe the bug

Using vite-plugin-ssr with Vue, running build with an asset referenced from the root path (e.g. /myimage.jpg) results in a Rollup error. This doesn't occur during dev.

Also, the issue doesn't occur when used with a generic Vue SPA.

However, @brillout noted that vite-plugin-ssr doesn't do anything about the public directory

Here's a detailed explanation of the scenarios:

Scenario A: VPS with Vue using npm init vite-plugin-ssr@latest

"vite": "^3.0.9",
"vite-plugin-ssr": "^0.4.32",
"@vitejs/plugin-vue": "^3.0.3"

myimage.jpg can be found in public directory, inside the root component in the template:

this breaks:

<img src="/myimage.jpg" />

this works:

<img src="http://localhost:3000/myimage.jpg" />

Scenario B: Vue 3 project using npm init vue@latest

"vite": "^3.0.9",
"@vitejs/plugin-vue": "^3.0.3"

myimage.jpg can be found in public directory, inside the root component in the template:

this works:

<img src="/myimage.jpg" />

this works:

<img src="http://localhost:3000/myimage.jpg" />

Reproduction

https://github.com/truumahn/vps-public-image-repro

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (12) x64 AMD Ryzen 5 4600H with Radeon Graphics
    Memory: 5.76 GB / 15.31 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.16.0 - /mnt/wslg/runtime-dir/fnm_multishells/31022_1662972639084/bin/node
    npm: 8.11.0 - /mnt/wslg/runtime-dir/fnm_multishells/31022_1662972639084/bin/npm
  Browsers:
    Chrome: 96.0.4664.45
  npmPackages:
    @vitejs/plugin-vue: ^3.0.3 => 3.1.0 
    vite: ^3.0.9 => 3.1.0

Used Package Manager

pnpm

Logs

Click to expand!
[vite]: Rollup failed to resolve import "/politecat.jpg" from "renderer/PageShell.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
[vite-plugin-ssr:autoFullBuild] [vite]: Rollup failed to resolve import "/politecat.jpg" from "renderer/PageShell.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
error during build:
Error: [vite]: Rollup failed to resolve import "/politecat.jpg" from "renderer/PageShell.vue?vue&type=script&setup=true&lang.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at onRollupWarning (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-665b0112.js:45824:19)
    at onwarn (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-665b0112.js:45622:13)
    at Object.onwarn (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:23225:13)
    at ModuleLoader.handleResolveId (file:///path/to/project/vite-ssr-project/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:22352:26)
    at file:///path/to/project/vite-ssr-project/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/rollup.js:22313:26
 ELIFECYCLE  Command failed with exit code 1.

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions