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

multiple <link /> tags get duplicated in <head /> #3169

Closed
1 task done
liweinan0423 opened this issue Oct 25, 2017 · 6 comments
Closed
1 task done

multiple <link /> tags get duplicated in <head /> #3169

liweinan0423 opened this issue Oct 25, 2017 · 6 comments

Comments

@liweinan0423
Copy link
Contributor

liweinan0423 commented Oct 25, 2017

Relates to #2438 .

If we add the same <link /> tag multiples in different <Head />, they are duplicated in the rendered the DOM

  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

multiple <link /> tags with exactly the same attributes should be du-duped when rendering HTML <head> elements

e.g.
component1.js

import Head from 'next/head'

<Head>
  <link rel="stylesheet" href="/static/style.css" />
</Head>

component2.js

import Head from 'next/head'
<Head>
  <link rel="stylesheet" href="/static/style.css" />
</Head>

Then the rendered HTML should be, where duplicated tags are unique'd

<html>
  <head>
    <link rel="stylesheet" href="/static/style.css" class="next-head" />
  </head>
</html>

Current Behavior

There will be duplicated tags rendered

<html>
  <head>
    <link rel="stylesheet" href="/static/style.css" class="next-head" />
    <link rel="stylesheet" href="/static/style.css" class="next-head" />
  </head>
</html>

Context

I am currently working on a solution with react-apollo which allows the data be pre-fetched before a new page is rendered at client side, the example with-apollo-redux only supports prefetching data at server side, therefore, at client side I have to walk through the whole component tree again and do data fetching if one node from the component tree has been wrapped by the graphql HOC.

Since the component tree contains <Head /> tags that produces side effects while going through the tree, so duplicated <link /> tags get inserted in the DOM. I was supposed to call Head.rewind() but it is not allowed at client side.

As I know react-helmet will not generate duplicated <link /> tags

I know we should leave user the liberty of inserting duplicated <link /> tags if it is desired. But I believe in most cases user doesn't want it. So I am wondering if it would be OK by adding a unique flag on the tags so the head.js would de-dupe tags with unique specified.

import Head from 'next/head'

<Head>
  <link rel="stylesheet" href="/static/style.css" unique />
</Head>
@timneutkens
Copy link
Member

@liweinan0423 I had a PR adding the React key property for next/head so you can dedupe tags, but we closed it as ZEIT didn't have a need for it anymore. Something like unique wouldn't work cause we'd have to dedupe all the tags manually while the user can just provide a key to make sure it's deduped, just like React does 👌 I'm not opposed to adding it 👍 What do you think about this proposal? We've talked about this quite extensive internally.

@liweinan0423
Copy link
Contributor Author

@timneutkens Thanks for your suggestion. I like the key property more than unique 😄 . I will try to send a PR.

@timneutkens
Copy link
Member

@liweinan0423 you don't have to, I already did it a while ago. Will pull the fix back in and fix it 👌

@liweinan0423
Copy link
Contributor Author

liweinan0423 commented Oct 25, 2017

@timneutkens After I finished my PR I saw your last comment. But could you review and feel free to close it and get in yours :)

@SBoudrias
Copy link
Contributor

Did this got removed again somehow?

I'm using next@^4.1.4 and I'm still seeing duplicated style tags:

screen shot 2017-11-27 at 4 43 40 pm
screen shot 2017-11-27 at 4 43 29 pm

() => (
    <div>
            <Head>
                <style
                    key="aa-react-select-css"
                    data-source="aa-react-select-css"
                    type="text/css"
                    dangerouslySetInnerHTML={{ __html: cssStyle.toString() }}
                />
            </Head>
            <Etc />
    </div>
);

@liweinan0423
Copy link
Contributor Author

liweinan0423 commented Nov 27, 2017

this change is in the canary branch now. please use next@canary for now

@lock lock bot locked as resolved and limited conversation to collaborators Nov 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants