Skip to content

IDE throws error on ?url&raw import #21277

@ben221199

Description

@ben221199

Description

Our website is made with VitePress and deployed on Cloudflare Pages. For redirects, we use Cloudflare's _redirects file, but this does only work on initial requests, not on secondary requests (due to single page) or during development.

To fix this, I imported the _redirects file:

import _redirects from "/_redirects?url&raw";
const redirects = _redirects
  .split("\n")
  .filter(String)
  .map((item: string[]) => item.split(/\x20+/))
  .filter(
    (item: string) =>
      item[0].indexOf(":") === -1 && item[0].indexOf("*") === -1,
  );

And then, inside the layout's enhanceApp function, I catch the redirects:

    router.onAfterRouteChange = async (to: string) => {
      // Static redirects
      redirects.forEach((item: string[]) => {
        if (
          to === item[0] ||
          to.startsWith(item[0] + "?") ||
          to.startsWith(item[0] + "#")
        ) {
          router.go(item[1]);
        }
      });
    };

This works fine, but I noticed that my IDE gave errors on the import line, caused by ?url&raw. I was not able to use ?raw directly, because of:

Image

Is there a reason why there isn't a declare module '*?url&raw' defined in Vite? Is a bug of Vite, or is my method badly wrong?

Suggested solution

Add:

declare module '*?url&raw' {
  const src: string
  export default src
}

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions