Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

Support configuring Html props from react-server #1661

Merged
merged 3 commits into from
Oct 24, 2020

Conversation

BPScott
Copy link
Member

@BPScott BPScott commented Oct 22, 2020

Description

react-html's <Html> component supports a bunch of props for configuring
content to inject into the head, however previously that was not exposed
from react-server's createRender and createServer functions.

This commit lets you pass a htmlProps option into createRender and
createServer that will then be applied when createRender calls <Html>.

This means you can use createServer, and inject arbitary content into
the head using <Html>'s headMarkup prop (and use any of <Html>'s other
props too):

createServer({
  render: () => <MockApp />,
  htmlProps: {
    scripts: [{path: '/extraScript.js'}],
    styles: [{path: '/extraStyle.css'}],
    headMarkup:  <>
      <script>let ScriptFromHeadMarkup = 1;</script>
      <script>let ScriptFromSecondHeadMarkup = 1;</script>
    </>,
  },
}),

To test

An hand-cranked end-to-end test of this might be hard as there probably aren't many demo apps lying around.

Find an application that uses createServer within it's server entrypoint (brand-registry does this but I'm otherwise unfamiliar with the app). Add 'htmlProps' to its list of options, and then note that the markup is injected into the head:

htmlProps: {
  headMarkup:  <>
    <script>let ScriptFromHeadMarkup = 1;</script>
  </>,
}

Fortunately the createServer tests act as automated end-to-end test by configuring the server and asserting what html gets returned. this new unit test demonstrates adding htmlProps to the call to createServer and asserting that the returned html includes that head markup.

So the real tophat instructions are "check tests pass, check the added unit tests showcase e2e behaviour" :D

Type of change

  • @shopify/react-html Patch: Bug (non-breaking change which fixes an issue)
  • @shopify/react-server Minor: New feature (non-breaking change which adds functionality)

Checklist

  • I have added a changelog entry, prefixed by the type of change noted above (Documentation fix and Test update does not need a changelog as we do not publish new version)

react-html's <Html> component supports a bunch of props for configuring
content to inject into the head, however previously that was not exposed
from react-server's createRender and createServer functions.

This commit lets you pass a htmlProps option into createRender and
createServer that will then be applied to the Html element.

This means you can use createServer, and inject arbitary content into
the head using <Html>'s headMarkup prop (and use any of <Html>'s other
props too)
manager?: HtmlManager;
hydrationManager?: HydrationManager;
children: React.ReactElement<any> | string;
children?: React.ReactElement<any> | string;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting HtmlProps and marking children as optional means I can reuse this type down in the definition of createRender's options object (https://github.com/Shopify/quilt/pull/1661/files#diff-3db342f047695e4ca4277cd8264ac3b2db110ec15ec6c34ed697ce2859004df0R57) rather than creating a new type that omits children

@@ -133,8 +148,16 @@ export function createRender(render: RenderFunction, options: Options = {}) {
assets.scripts({name: assetName, asyncAssets: immediateAsyncAssets}),
]);

styles.push(...additionalStyles);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that if somebody decides to use styles/scripts then they don't clobber the assets provided by getAssets. I've added a test case for this.

@@ -18,10 +17,11 @@ interface Options {
port?: number;
assetPrefix?: string;
proxy?: boolean;
assetName?: string | ValueFromContext<string>;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking the change to assetName and renderError isn't needed as this is functionally identical, but I figure reusing the existing type is nicer than having to repeat the declarations.

@kaelig
Copy link
Contributor

kaelig commented Oct 22, 2020

Would it make sense to add an example in the README, showing what's now possible to do?

@BPScott
Copy link
Member Author

BPScott commented Oct 23, 2020

styles: additionalStyles = [],
...additionalHtmlProps
} =
typeof htmlPropsInput === 'function'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit nitpicky but I found this a bit difficult to parse at first glance because of the line break.
Maybe split this up into 2 expressions by pulling out this ternary into a const props or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, the linebreak after the = is quite unconventional. Not sure it's worth adding a level of indirection to fix this, though.

Copy link
Contributor

@ismail-syed ismail-syed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be hard as there probably aren't many demo apps lying around.

A majority of our createServer use cases are abtracted behind the react-server webpack plugin. I don't foresee any impact on those cases with this PR (I didn't tophat). 🎩ing this is challenging without a beta release of some sort. If you feel confident with these changes, that's 👍 with me. If you want to run beta release, I can give this a 🎩 .

Copy link
Contributor

@kaelig kaelig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the README update, I approve the README side of things! 👍

@BPScott BPScott merged commit a3265bf into master Oct 24, 2020
@BPScott BPScott deleted the html-config-from-react-server branch October 24, 2020 01:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants