Do you ever build static HTML concurrently with a React app?

A lot of the times React is taught in a way where you make your entire page as a stand-alone from everything but the App “root” in the HTML. But are there times you would want to use mostly static content, and an actual React app somewhere as a small piece of that static content? Maybe for SEO, in the similar reason in how landing pages are separate from React apps?

Posted May 24, 2019, 7:04 AM UTC

314
arcticicestudio•

I'd never build websites without Gatsby anymore. It comes with the latest tech stack and best practices for the JAMStack out-of-the-box and is mainly focused to create highly performant static (SSR) websites based on React while allowing to use dynamic content or even make some paths client-only (PWA/App).

3
titratecodeOP•

Oh wow, that sounds exactly like the solution to my React quips. That is going first on my list of front end techs to learn.

1
arcticicestudio•

Nice to hear I could convince you 😄 You won't be disappointed, Gatsby is the state-of-the-art for the JAMStack. There are ten thousands of sites already using Gatsby, from small personal pages up to commercial shops. Even the official React website and documentations are build with Gatsby as well as the blog of the React core team member Dan Abramov. If you've installed the official React DevTools Browser Extension you can easily see which websites using React and when opening up your DevTools you'll be surprised how many page's root element have the id attribute __gatsby 😉

2
Herm_af•

Yep I'm using it for our main site at work right now. All the optimization is amazing. Even my CEO is like "this is awesome!"

1
Wowitstheinternet•

There are occasional situations that warrant this, like embedding a modern application within an older website.

You'd spin up a second website, separate from the static site, and use <iframe> tags to embed the React site on the older static site.

1
titratecodeOP•

Oh wow! Do you know of an example of a site that has done something like that?

1
Wowitstheinternet•

Err, not <script> tags, sorry. I meant <iframe> tags.

​

And sure, here's something you could drop into your own codebase right now.

https://www.w3schools.com/tags/tag_iframe.asp

Basically the idea here is you call another webpage, from within your current webpage.

2
walidh93•

I have done exactly the same

http://ydl-me.com/test-menu

2
titratecodeOP•

I'm just curious as to what was the reason for doing this? It seems you only wrote the Nav bar and footer in HTML, but still very nice example.

1
walidh93•

The site was built in OctoberCMS a while ago, all content is managed from October dashboard ... didn’t want to build the whole site with react again ... just built the dynamic search component and linked it with iframe.

2
montas•

We are developing an online purchase process app. This will be done as an SPA that will run separately from marketing site. On marketing site they wanted some basic functionality, like same search form as in the spa, but embeded in cms.

We are creating separate entries for some components which will enable the marketing team to just put some markup into any marketing page and react component from purchase process will be mounted into it. You can make the mounting process even pass props to the component based on data attributes on wrapper.

For example they can use data-facebook-login on any div and they will get working, fully configured facebook login button. Or data-cancle-ticket and component handling whole cancelation process will be mounted in place.

All of these "packs" have separate js and css files. Since marketing site uses same css kit as the purchase process, they dont need to include css files, just js, and they will get designed working components.

1
titratecodeOP•

Sorry that kind of went over my head. Your app is a SPA in the form of a button that you mount to virtually any site, and it changes e-commerce functionalities based on the data that is passed to it? If I’m understanding correctly, that’s brilliant.

1
montas•

We have multiple js "packs" (or chunks or entrypoints).

For example, we have application.js which is build from following

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import Application from 'modules/application';

document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('[data-application]').forEach(target => {
        ReactDOM.render(<Application />, target);
    });
});

Application includes everything you would expect of SPA. It includes router, application context, graphql client, all the pages of SPA, all the components needed for it to work (including ticket cancelation process and components, or facebook login button).

Next we have customer.js, which look something like this

import '@uikit/@base/global.scss';

import '@uikit/Check/index.scss';
import '@uikit/ContentSwitcher/index.scss';
import '@uikit/EcareMenu/index.scss';
import '@uikit/Narrow/index.scss';
import '@uikit/ScrollContainer/index.scss';
import '@uikit/Sidebar/index.scss';
import '@uikit/Thin/index.scss';
import '@uikit/Ticket/index.scss';
import '@uikit/TicketList/index.scss';

import * as printJS from 'print-js';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

import Icon, { ICON_NAME } from '@uikit/Icon';
import Indicator from '@uikit/Indicator';
import PasswordInputField from '@uikit/InputField/PasswordInputField';
import FacebookLoginButton from 'components/FacebookLoginButton';
import Footer from 'components/Footer';
import CustomerLanguagePicker from 'components/LanguagePicker/CustomerLanguagePicker';
import CustomerNavbar from 'modules/customer/CustomerNavbar';

document.addEventListener('load', () => {
    document.querySelectorAll('[data-navbar]').forEach(target => {
        ReactDOM.render(<CustomerNavbar />, target);
    });

    document.querySelectorAll('[data-footer]').forEach(target => {
        ReactDOM.render(<Footer />, target);
    });

    document.querySelectorAll('[data-icon]').forEach(target => {
        const name = target.getAttribute('data-icon') as ICON_NAME;
        ReactDOM.render(<Icon name={name} raw />, target);
    });

    document.querySelectorAll('[data-language-picker]').forEach(target => {
        ReactDOM.render(<CustomerLanguagePicker />, target);
    });

    document.querySelectorAll('[data-facebook-login]').forEach(target => {
        ReactDOM.render(<FacebookLoginButton />, target);
    });

    document.querySelectorAll('[data-password-field]').forEach(target => {
        const input = target.querySelector('input');
        const attributes = input && input.attributes;
        ReactDOM.render(<PasswordInputField raw={attributes} />, target);
    });

    document.querySelectorAll('[data-print-url]').forEach(target => {
        const url = target.getAttribute('data-print-url');
        target.addEventListener('click', () => printJS(url ? url : ''));
    });

    document.querySelectorAll('[data-indicator]').forEach(target => {
        ReactDOM.render(<Indicator />, target);
    });
});

This is heavy work in progress :) need to refactor it a lot. But the point is, marketing team can include this customer.js file in marketing pages and use

<div data-navbar></div>

and they will get the navbar component in that place. As you can see, we have a few more components done like this. For example they can use <div data-icon="customer"></div> and we will render customer icon inplace.

1
titratecodeOP•

Oh! That’s an excellent use case. I’m guessing that your customer.js components each render data-application divs. Jesus, I didn’t even know that React apps could be modularized that extensively.

1