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

It is not possible to re-import after a failed import. #17351

Closed
4 tasks done
mench32 opened this issue May 30, 2024 · 1 comment
Closed
4 tasks done

It is not possible to re-import after a failed import. #17351

mench32 opened this issue May 30, 2024 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@mench32
Copy link

mench32 commented May 30, 2024

Description

If the dynamic module import fails, for example due to problems with the Internet connection, it will be cached and will never be able to import again.

Example

const button = document.getElementById('btn');

button.onclick = async () => {
  try {
    const module = await import(`./module.js`);
    console.log(module.calc(1, 2));
  }
  catch (err) {
    console.log(err?.message); // <-- after fail we will always catch error
  }
}

In native ESM we can add search like "?c=1" params to the end of the string:

import('./module.js?c=1')

and it will solve the problem

I didn't find a solution in the Vite documentation.

Suggested solution

Add support for dynamic search:
const module = await import(`./dir/${file}.js?c=${count}`)

Alternative

  1. Do not cache unsuccessfully loaded modules (Can be optional).
  2. Add a special comment similar to /* @vite-ignore */ (for example /* @vite-nocache */)
  3. Add some method for clearing cache vite import.meta.clearImportCache('./module.js')

Additional context

No response

Validations

@sapphi-red
Copy link
Member

sapphi-red commented Jun 17, 2024

Duplicate of #11804. See #11804 (comment) for why Vite cannot add params to burst the cache.

It seems whatwg/html#6768 has some progress recently, so we might have a better solution to this in near future.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Jun 17, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed enhancement: pending triage labels Jun 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants