question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating a unique id is impossible in Nuxt 3

See original GitHub issue

Environment

sandbox@sse-sandbox-u1udu3:/sandbox$ npx nuxi info
Nuxt CLI v3.0.0-27424151.dbab979
RootDir: /sandbox
Nuxt project info:

------------------------------
- Operating System: `Linux`
- Node Version:     `v14.18.1`
- Nuxt Version:     `3.0.0-27424151.dbab979`
- Package Manager:  `yarn@1.22.17`
- Bundler:          `Vite`
- User Config:      `-`
- Runtime Modules:  `-`
- Build Modules:    `-`
------------------------------

👉 Report an issue: https://github.com/nuxt/framework/issues/new

👉 Suggest an improvement: https://github.com/nuxt/framework/discussions/new

👉 Read documentation: https://v3.nuxtjs.org

sandbox@sse-sandbox-u1udu3:/sandbox$

Reproduction

Reproduction URL

If you don’t see the issue at first, try refreshing the preview.

Describe the bug

When attempting to create a unique identifier in a component (using Options API, Composition API, or setup sugar), The id changes between the server and the client.

This makes it impossible for components that require access to a unique HTML id (for accessibility reasons or other) and causes hydration issues.

Additional context

No response

Logs

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jshimkoskicommented, Feb 21, 2022

Thank you. I’ll start up a discussion thread.

Regarding the “why do you need unique IDs” question:

Unique IDs are a pretty big part of accessibility or being able to reference different parts of the DOM for libraries such as popperjs.

One small example related to accessibility is aria-labelby:

<template>
  <span role="checkbox" aria-checked="false" tabindex="0" aria-labelledby="tac"></span>
  <span id="tac">I agree to the Terms and Conditions.</span>
</template>

If you have multiple instances of a component with that code, it breaks the HTML since an id must always be unique.

This issue doesn’t stop there and it’s a pretty common necessity (just do a simple google search to see what I mean).

I wish component instance IDs were baked into Vue 3. It would solve all of this.

Thanks again.

1reaction
danielroecommented, Feb 21, 2022

This didn’t happen in Nuxt 2.

This also throws an error in Nuxt 2: https://codesandbox.io/s/async-cookies-ur28r1?file=/pages/index.vue.

I’m closing this as it’s not an error but a consequence of client-side hydration, but I’d recommend you create a discussion with some more information on what you’re trying to do, what your key needs are, and we can think about how to achieve that. (For example, why do you need unique IDs?)

Note: You may find this section of the Vue docs helpful in answering some of your questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unique IDs for component instances that work in SSR #3348
Having unique identifiers across component instances is an absolute must. The server will always assign id different to what the client ...
Read more >
Vue.js: How to set a unique ID for each component instance?
The simplest way I found was to create a UUID ( uuid package ) manually through a global mixin. That way you won't...
Read more >
The build Property - Nuxt
Nuxt lets you customize the webpack configuration for building your web ... Type: number | string (Valid values are 'auto' , 2 and...
Read more >
Nuxt 3 State Management: Pinia vs useState | Vue Mastery
A user's unique id or accountId; A list of features or permissions the current user can access; Color themes, whether dark mode is...
Read more >
3 efficient ways to generate UUID in Node.js
UUID can be very useful as reliable unique identifiers. In this post, you will learn how to generate a UUID using Node.js and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found