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

allow use-credentials of crossorigin attribute #6648

Open
4 tasks done
CedricHildbrand opened this issue Jan 26, 2022 · 8 comments
Open
4 tasks done

allow use-credentials of crossorigin attribute #6648

CedricHildbrand opened this issue Jan 26, 2022 · 8 comments

Comments

@CedricHildbrand
Copy link

Clear and concise description of the problem

When building, the script tags inside the dist index.html get the attribute crossorigin automatically. The only way to change it to "use-credentials" is by modifying the final dist file.
The reason this should be able to be modified is because there is a bug in Safari which leads to an 401 HTTP error if you have any authentication. https://bugs.webkit.org/show_bug.cgi?id=171566

Suggested solution

It would be nice to set crossorigin="use-credentials" inside vite.config.js somewhere for modules.

Alternative

No response

Additional context

No response

Validations

@sapphi-red
Copy link
Member

related: #4453

@bluwy
Copy link
Member

bluwy commented Jun 25, 2022

I'm not sure about allowing another set of config to workaround a browser bug, since the issue has been 2 years and counting, perhaps it's fine to follow the correct spec instead. Another solution is to check build.target for a safari version and set use-credentials accordingly.

@JSFiend
Copy link

JSFiend commented Sep 26, 2022

create a vite plugin to replace

function useCredentials() {
  return {
    name: 'use-credentials',
    transformIndexHtml(html: string) {
      return html.replace(
        'crossorigin',
        'crossorigin="use-credentials"',
      );
    },
  };

@ketulm
Copy link

ketulm commented Jun 5, 2023

There is a similar issue on Firefox.

The above plugin workaround wouldn't work for <link> tags added by html.ts for pre-loading the bundle. The modulepreload polyfill on Firefox defaults to anonymous credentials for these pre-load links.

It'd be good to handle non-chrome browsers in the polyfill or add a vite option to allow explicitly setting the crossorigin attribute.

Are there any plans to address this soon? Our production builds are affected by this. Currently, working around it with forked Vite with html.ts patched alongside the #13136 feature.

@electrovir
Copy link
Sponsor

create a vite plugin to replace

function useCredentials() {
  return {
    name: 'use-credentials',
    transformIndexHtml(html: string) {
      return html.replace(
        'crossorigin',
        'crossorigin="use-credentials"',
      );
    },
  };

I was looking for a way to remove the crossorigin attribute entirely and this helped a lot, thanks!

@ikeq
Copy link
Contributor

ikeq commented Mar 7, 2024

The solution addressed by @JSFiend will not work for lazy loaded modules (via import()).

image

Source code:

Still need an official solution @bluwy

@ikeq
Copy link
Contributor

ikeq commented Mar 7, 2024

Came up with this temporary workaround

export default defineConfig({
  plugins: [
    {
      name: 'crossorigin',
      transformIndexHtml(html) {
        return html.replace(/crossorigin/g, 'crossorigin="use-credentials"');
      },
    },
    {
      generateBundle(options, bundle) {
        for (const url in bundle) {
          // 2. Then replace `crossOrigin`
          if (bundle[url].name === 'helper') {
            bundle[url].code = bundle[url].code.replace(
              'crossOrigin=""',
              'crossOrigin="use-credentials"'
            );
          }
        }
      },
    },
  ],
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          // 1. Split vite scripts into a separate chunk
          if (id.startsWith('\u0000vite')) {
            return 'helper';
          }
        },
      },
    },
  },
});

@front-refined
Copy link

想出了这个临时解决办法

export default defineConfig({
  plugins: [
    {
      name: 'crossorigin',
      transformIndexHtml(html) {
        return html.replace(/crossorigin/g, 'crossorigin="use-credentials"');
      },
    },
    {
      generateBundle(options, bundle) {
        for (const url in bundle) {
          // 2. Then replace `crossOrigin`
          if (bundle[url].name === 'helper') {
            bundle[url].code = bundle[url].code.replace(
              'crossOrigin=""',
              'crossOrigin="use-credentials"'
            );
          }
        }
      },
    },
  ],
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          // 1. Split vite scripts into a separate chunk
          if (id.startsWith('\u0000vite')) {
            return 'helper';
          }
        },
      },
    },
  },
});

nice 👍 I made a change based on this, others can also refer to it

plugins: [
  {
    name: 'vite:crossorigin-use-credentials',
    transformIndexHtml(html) {
      return html.replace(/crossorigin/g, 'crossorigin="use-credentials"');
    },
    generateBundle(_, bundle) {
      for (const url in bundle) {
        if (bundle[url].name === 'preload-helper') {
          // @ts-ignore
          bundle[url].code = bundle[url].code.replace(
            'crossOrigin = ""',
            'crossOrigin = "use-credentials"'
          );
        }
      }
    }
  }
],
build: {
  rollupOptions: {
    output: {
      manualChunks(id) {
        // Extract virtual module
        if (id === '\0vite/preload-helper.js') {
          return 'preload-helper';
        }
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants