Skip to content

Injecting Data from Server #1703

Closed
Closed
@Timer

Description

@Timer

Our docs explain how to Inject Data from the Server, but this doesn't translate well to development.

I think we should expand the docs to explain what to do in development or add means of specifying these values in development mode.

Right now I just live with the error and do something like this:

if (window.ActiveDirectoryGroups === undefined) window.ActiveDirectoryGroups = ['devmode']

This will be annoying for people using this suggested method when the error dialog is merged (if they're currently ignoring the error).

Activity

added this to the 0.10.0 milestone on Mar 3, 2017
Timer

Timer commented on Mar 3, 2017

@Timer
ContributorAuthor

I'm going to tag this for 0.10 just so we don't forget, but if this is unactionable that's fine too.

evenchange4

evenchange4 commented on Mar 20, 2017

@evenchange4
Contributor

How about using try-catch so that we can handle it safely in development?

<script>
  try {
   window.SERVER_DATA = __SERVER_DATA__;
  } catch(e) {
    console.info('Development MODE', e)
    window.SERVER_DATA = {};
  }
</script>
modified the milestone: 0.11.0 on May 11, 2017
gaearon

gaearon commented on May 11, 2017

@gaearon
Contributor

Retagging to clean up 0.10.

peterbe

peterbe commented on Jun 16, 2017

@peterbe

Another idea is to use a script src. E.g. <script src="data.json"></script> (careful about ordering and 'async' attributes). In dev mode, that'd query http://localhost:3000/data.json which would probably 404 Not Found (unless it's proxied). In production you'd have your web server pick that up and respond something like

window.SERVER_DATA = {some: 'thing'};

Then the web server doesn't need to do some something like this:

# pseudo python server code

def render_homepage(request):
    with open('build/index.html') as f:
          html = f.read()
          html = html.replace('__SERVER_DATA__', "{some: 'thing'};")
    return response(html)
Vadorequest

Vadorequest commented on Jun 16, 2017

@Vadorequest

@peterbe I guess you mean a data.js file? I doubt we can load JSON through script, never saw that before.

leoskyrocker

leoskyrocker commented on Jul 10, 2017

@leoskyrocker

@peterbe Wouldn't that require another request to the server?

cr101

cr101 commented on Jul 10, 2017

@cr101
Contributor

@LeoI11 Be aware that injecting data from the server has a major drawback as it will not allow search engines to properly analyze the static HTML.

piotr-cz

piotr-cz commented on Oct 26, 2017

@piotr-cz
Contributor

Data injected with proposed <script> tag won't be cached with serviceworker in production and won't be available when offline.

My current approach is:

index.html

<script>
  window.APP_BUILD = {
    HASH: '__APP_BUILD_HASH__',
  }
</script>

App.js

const buildHash = !window.APP_BUILD.HASH || window.APP_BUILD.HASH === '__APP_BUILD_HASH__'
  ? undefined
  : window.APP_BUILD.HASH
locked and limited conversation to collaborators on Jan 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @peterbe@piotr-cz@Timer@gaearon@evenchange4

        Issue actions

          Injecting Data from Server · Issue #1703 · facebook/create-react-app