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.

__vitePreload dependencies could be relative to import.meta.url

See original GitHub issue

Describe the bug

__vitePreload includes assumptions about where assets are being served from. In a normal Vite project those assumptions hold true, but in more bespoke situations they may not.

Additionally, in the case where CSS dependencies fail to load (whether due to a network failure, or because of the situation described above) the promise returned from __vitePreload will never resolve.

Reproduction

https://github.com/Rich-Harris/vite-preload-repro

Given sources like this…

// src/index.js
import('./foo.js').then((foo) => {
  foo.hello();
});
// src/foo.js
import './foo.css';

export function hello() {
  document.body.innerHTML = `<h1>Hello!</h1>`;
}
// src/foo.css
h1 {
  color: red;
}

…and a config like this…

const config = {
  build: {
    cssCodeSplit: true,
    lib: {
      entry: resolve('src/index.js'),
      name: 'app', // this doesn't seem to do anything?
      formats: ['es']
    },
    outDir: 'dist',
    minify: false,
    rollupOptions: {
      output: {
        entryFileNames: '[name]-[hash].js',
        chunkFileNames: '[name]-[hash].js',
        assetFileNames: '[name]-[hash][extname]'
      }
    }
  }
};

…the resulting dist/index-ed8ec7ff.js file contains this line:

__vitePreload(() => import("./foo-5368ca0f.js"), true ? ["/foo-5368ca0f.js","/foo-0a0d0fb0.css"] : void 0).then((foo) => {

In the case where assets are served from somewhere other than / (for example, on an external CDN), the /foo-5368ca0f.js and /foo-0a0d0fb0.css paths are meaningless. If they were relative paths instead, the implementation of __vitePreload could add the following…

  return Promise.all(deps.map((dep) => {
+   dep = new URL(dep, import.meta.url).href;
    if (dep in seen)
      return;

…and the resulting files would be more portable.

Meanwhile, to prevent the promise from hanging:

    if (isCss) {
-     return new Promise((res) => {
-       link.addEventListener("load", res);
+     return new Promise((res, rej) => {
+       link.addEventListener("load", res);
+       link.addEventListener("error", rej);
      });
    }

System Info

  • vite version: 2.0.0-beta.69
  • Operating System: Mac OS
  • Node version: 12.18.3
  • Package manager (npm/yarn/pnpm) and version: npm 6.14.6

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
benmccanncommented, Mar 1, 2022

Actually, in my case base should suffice

You can set the base path now, but it’s turned out not to be sufficient for the SvelteKit use case. It’s still be nice to remove that and make it relative. E.g. for deploying to IPFS and other targets as described in https://github.com/sveltejs/kit/issues/595

1reaction
eigancommented, Feb 15, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Build Options - Vite
By default, an absolute path including the base will be used when loading these dependencies. If the base is relative ( '' or...
Read more >
rollup-plugin-import-meta-url-to-module - npm
Because in Server-Side Rendering, import.meta.url is a file: protocol URL, if the base option in vite config does not include protocol and ...
Read more >
Asset Bundling (Vite) - The PHP Framework For Web Artisans
The Laravel Vite plugin requires you to specify the entry points for your application. These may be JavaScript or CSS files, and include...
Read more >
React project setup with Vite for faster development
import.meta.url is a native ESM feature that exposes the current module's URL. Combining it with the native URL constructor, we can obtain the ......
Read more >
uikit - How can vite be configured to load relative images in ...
This tells Vite how to resolve the problematic relative image paths. ... node_modules/uikit/src/images' + updatedId, import.meta.url ) ); } ...
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