Skip to content

Crossorigin on styles not getting set #7953

Description

@perljusback

Error on credentials on preload for stylesheets in Chrome 84

image

Versions

  • nuxt: 2.14.3
    Mode: Universal
    Target: Server
  • node: 14.6.0

Reproduction

when using these render settings:

  render: {
    compressor: false,
    crossorigin: 'anonymous',
    http2: {
      push: true
    },
    resourceHints: true
  },

crossorigin gets set to the link header and to the preload link in the html:
image

image

but the stylesheet itself doesn't get the crossorigin markup:
image

Looking through the code it seems a replace is done for all the scripts:
https://github.com/nuxt/nuxt.js/blob/dev/packages/vue-renderer/src/renderers/ssr.js#L23

  renderScripts (renderContext) {
    const scripts = renderContext.renderScripts()
    const { render: { crossorigin } } = this.options
    if (!crossorigin) {
      return scripts
    }
    return scripts.replace(
      /<script/g,
      `<script crossorigin="${crossorigin}"`
    )
  }

and if we add a similar one for Styles

  renderStyles (renderContext) {
    const styles = renderContext.renderStyles();
    const { render: { crossorigin } } = this.options;
    if (!crossorigin) {
      return styles
    }
    return styles.replace(
      /<link/g,
      `<link crossorigin="${crossorigin}"`
    )
  }

and change:
https://github.com/nuxt/nuxt.js/blob/dev/packages/vue-renderer/src/renderers/ssr.js#L147

    // Inject styles
    HEAD += renderContext.renderStyles()

into

    // Inject styles
    HEAD += this.renderStyles(renderContext);

The error goes away

Thanks for all the great work.

Activity

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

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