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

Google fonts are loaded as <script> instead of <link> #174

Closed
lbogdan opened this issue Sep 3, 2017 · 14 comments
Closed

Google fonts are loaded as <script> instead of <link> #174

lbogdan opened this issue Sep 3, 2017 · 14 comments

Comments

@lbogdan
Copy link
Contributor

lbogdan commented Sep 3, 2017


@lbogdan lbogdan changed the title Google fonts are loaded as script instead of link Google fonts are loaded as <script> instead of <link> Sep 3, 2017
@CompuIves
Copy link
Member

Ooh good one, logic for this resides here: https://github.com/CompuIves/codesandbox-client/blob/master/src/sandbox/external-resources.js.

Maybe we should either do a manual check, or do a preflight request to find out the Content Type?

@lbogdan
Copy link
Contributor Author

lbogdan commented Sep 4, 2017

It would be nice if the API could figure out the resource type when you add it (even return an error if it's neither JS, nor CSS), and return it. I don't know about the backwards compatibility, though.

@tansongyang
Copy link
Contributor

I'm interested in working on this.

It seems like you could just use a HEAD request and then check the returned Content-Type. Example pseudocode:

function addResource(resource: string) {
  const match = resource.match(/\.([^.]*)$/);

  if (match && match[1] === 'css') {
    addCSS(resource);
  } else if (match && match[1] === 'js') {
    addJS(resource);
  } else {
    head(resource).then((response) => {
      // Check reponse
      if (contentType(response) === 'text/css') {
        addCSS(resource)
      } else {
        addJS(resource);
      }
    }).catch((e) => {
      // Should we log an error?
      addJS(resource)
    })
  }
}

Some questions:

  1. Does calling code expect addResource to be synchronous?
  2. Is there a standard way of doing resource fetching in this project?

@CompuIves
Copy link
Member

That's really great @tansongyang! Your proposal sounds great.

Answering the questions:

  1. addResource can be asynchronous, everything in the sandbox itself is executed in an asynchronous manner
  2. For the sandbox we don't have a standard way of fetching, mostly to keep the bundle size low. I'd use window.fetch to fetch the resource.

I think we should log the error using console.error, mostly developers use CodeSandbox and maybe they can include it in the bug report when they occur this.

@tansongyang
Copy link
Contributor

@CompuIves OK. One possible issue with fetch is that it isn't supported in IE11. Are you OK with codesandbox not supporting IE11?

@CompuIves
Copy link
Member

@tansongyang No problem! We polyfill fetch, so it should also work on IE11.

@lbogdan
Copy link
Contributor Author

lbogdan commented Sep 12, 2017

Maybe also check for javascript Content-Type and error otherwise?

@CompuIves
Copy link
Member

CompuIves commented Sep 12, 2017 via email

@tansongyang
Copy link
Contributor

Sounds good. I'll post here when I have a PR.

@tansongyang
Copy link
Contributor

Made PR. See the comments and commit messages for more details.

@eschafer
Copy link

As a workaround, it looks like you can just add '.css' to the end of the url and it'll work: https://fonts.googleapis.com/css?family=Roboto:400,700.css

@mrchief
Copy link

mrchief commented Mar 5, 2018

Same thing happens with Material icons: https://fonts.googleapis.com/icon?family=Material+Icons. And adding .css to the URL doesn't work.

@eschafer
Copy link

eschafer commented Mar 5, 2018

If you add the weight, it works:
https://fonts.googleapis.com/icon?family=Material+Icons:400.css

@lbogdan
Copy link
Contributor Author

lbogdan commented May 1, 2018

Closing this as we can now load scripts and style-sheets from index.html.

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

5 participants