airbnb / hypernova

A service for server-side rendering your JavaScript views
MIT License
5.82k stars 208 forks source link

Make DATA_KEY and DATA_ID attributes importable #159

Closed marconi1992 closed 4 years ago

marconi1992 commented 4 years ago

Context:

Currently, I'm working on a framework using Hypernova and I've been writing some custom bindings for other libraries like Vue.js, Svelte, etc.

I'm getting the data-* values, but I think I need a way to warranty that the data attributes will be the same that hypernova is using if they change in the future.

https://github.com/ara-framework/hypernova-redom/blob/feat/render-client/src/index.js#L18

ljharb commented 4 years ago

Can you elaborate a bit more on why this is needed?

(It'd also need tests and docs)

marconi1992 commented 4 years ago

Sure @ljharb. For example, hypernova provides the fromScript helper that I'm using in my library.

There's no way to know which are the correct keys. They need to be hardcoded.

fromScript({
    `hypernova-key`: key,
    `hypernova-id`: id,
  });

I think is a good practice to export them to do something like this:

import { DATA_KEY, DATA_ID } from 'hypernova'

fromScript({
    [DATA_KEY]: key,
    [DATA_ID]: id,
 });

Otherwise, the helper is prone to errors if the developer makes a typo. Does it make sense?

ljharb commented 4 years ago

@marconi1992 that makes perfect sense! I'd be happy to merge this after some basic tests and docs are added.

marconi1992 commented 4 years ago

@ljharb I added a simple test and how to use them in the README.md. Let me know if something is missing.