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

How to get and serve HTML fragments? #9

Open
napolux opened this issue Nov 20, 2019 · 3 comments
Open

How to get and serve HTML fragments? #9

napolux opened this issue Nov 20, 2019 · 3 comments

Comments

@napolux
Copy link

napolux commented Nov 20, 2019

I was wondering how can I render an HTML fragment which is not a component...

For example:
Let's say that example.com/esi-fragment is serving

<div> content </div>

So, as per ESI i can do something like this in an example.com page

<esi:include src="/esi-fragment"/>

And I should see the div rendered in my page.

In all the examples I see online I only can see react components rendered... Which is working, but not really what I want to do.... Is there something I'm getting wrong?

Thanks!

@dunglas
Copy link
Owner

dunglas commented Nov 20, 2019

Unfortunately it cannot work like that because client-side React will try to re-conciliate with the HTML that has been generated sever side.
If you're not using a React component, when the client-side React will execute, you'll got a warning and the extra HTML (the content of the ESI block) will be discarded.

It's why the fragment must be a React component.

@napolux
Copy link
Author

napolux commented Nov 21, 2019

I actually came up with this, which works, but... Will your assumption be still valid?

Unfortunately it cannot work like that because client-side React will try to re-conciliate with the HTML that has been generated sever side.

export default class Header extends React.Component<HeaderProps> {
    render() {
        const htmlFragment = {__html: this.props.dataFromMicroservice};
        return (
            <section>
                <div dangerouslySetInnerHTML={htmlFragment} />
            </section>
        );
    }

    static async getInitialProps({ res }) {

        // this is where will retrieve HTML from microservice, now just a placeholder
        const data = await fetch('https://www.example.com'); 
        const html = await data.text();

        return new Promise(resolve => {
            if (res) {
                // Set a TTL for this fragment
                res.set('Cache-Control', 's-maxage=60, max-age=30');
            }

            resolve({
                dataFromMicroservice: html
            })
        });
    }

Which wil be used like...

const HeaderESI = withESI(Header, 'id');
...
...
...
<HeaderESI />

@jaszczw
Copy link

jaszczw commented Apr 20, 2020

@napolux This would work as whatever is inside dangerouslySetInnerHTML won't be tracked by React.

Unfortunately it cannot work like that because client-side React will try to re-conciliate with the HTML that has been generated sever side

Doesn't apply in you sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants