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.

Is the `rewind` approach safe?

See original GitHub issue

I’m wondering how the rewind feature works internally. As far as I understand it simply outputs and removes the last rendered data that can be rendered on the server into a head. So is there something like a queue inside that the head data is being pushed on?

This assumes that the node.js server calls rewind as soon as possible after the render, right? Couldn’t it happen that a new request comes in after a render that kicks off a new render and the rewind for the first request returns the data for the second request?

I might be totally wrong though – thanks in advance for the explanation! Very useful library btw.!

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:33 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
amannncommented, Dec 29, 2016

Thanks for the fast reply!

I did a bit more research and I found some related issues (seems like my concern is related to the underlying API of react-side-effect):

The important takeaway for me is that there should only be synchronous logic between renderToString and the call to rewind(), as async logic will introduce the race condition.

There are however also data fetching libraries that are rendering continuously in order to recursively fetch data for all components in the tree that need data (e.g. react-apollo does it – which is what I’m using – and also react-resolver I think). As far as I understand the node.js event loop, for these scenarios the data in Helmet can be inconsistent as the renders that are part of the data fetching can run in parallel – or rather parts of them after each other. Thus it’s important that there’s a final renderToString call after all the data fetching so the data for react-helmet will get into a correct state and can be pulled out.

I explained my findings in a bit more detail as maybe they could be helpful to someone else who is wondering about the same problem 😉.

3reactions
kouhincommented, Feb 9, 2018

I forked this repository and create react-safety-helmet to resolve this problem.

Everyone can simply migrate codes.

import { createHelmetStore, HelmetProvider, Helmet } from 'react-safety-helmet';

// helmetStore must be created per request.
const helmetStore = createHelmetStore();
ReactDOMServer.renderToString(
    <HelmetProvider store={helmetStore}>
        <Helmet>
            <title>Fancy title</title>
        </Helmet>
    </HelmetProvider>,
    container
);
const head = helmetStore.rewind();

Also, React 16 renderToNodeStream is supported.

import stream from 'stream';

class DrainWritable extends stream.Writable {
    constructor(options) {
        super(options);
        this.buffer = '';
    }

    _write(chunk, encoding, cb) {
        this.buffer += chunk;
        cb();
    }
}

new Promise((resolve, reject) => {
    const writable = new DrainWritable();
    const helmetStore = createHelmetStore();
    ReactDOMServer.renderToNodeStream(
        <HelmetProvider store={helmetStore}>
          <App />
        </HelmetProvider>,
    ).pipe(writable);

    writable.on('finish', () => {
        resolve({
            body: writable.buffer,
            head: helmetStore.rewind(),
        });
    });
    writable.on('error', reject);
}).then(({body, head}) => {
    // Create html with body and head object
});

If possible, I will create a PR to react-helmet, but it will break the compatibility.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rewind Technique - Psychology Tools
Clients should be cautious if their therapist is only proficient in the rewind technique and does not have trauma-specific CBT or EMDR training....
Read more >
Advantages and Disadvantages of the “Rewind Technique”
Proponents of the “Rewind Technique” tout it as a safe and “startlingly effective” method that reduces post-traumatic symptoms in over 70% ...
Read more >
The rewind technique - fast phobia and trauma treatment
The great thing about this technique is that it is completely safe and does not come with the risks of re-traumatising the client,...
Read more >
How Rewind Therapy can help people with PTSD
When you undergo Rewind therapy, the trained therapist will help you to become totally relaxed and immersed in a 'safe space'. This involves...
Read more >
The Rewind Technique And Why It Works - Step By Step
The Rewind Technique is an approach to working with trauma, especially individual traumas such as a road traffic accident. Clients who suffered anxiety, ......
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