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

[Documentation] hello upward tutorial #1080

Merged
merged 12 commits into from
Apr 4, 2019

Conversation

jcalcaben
Copy link
Contributor

Description

Creates a three part tutorial that show a "from scratch" approach of creating a web application backed by an UPWARD server.

Related Issue

Closes #1046

Motivation and Context

There have been a lot of questions on how people can get started from scratch.
It also helps to teach basic UPWARD concepts.

Verification Steps

React project created following the steps described in the tutorials.

How Has This Been Tested?

n/a

Screenshots / Screen Captures (if appropriate):

n/a

Proposed Labels for Change Type/Package

documentation
devdocs

Checklist:

  • I have read the CONTRIBUTING document.
  • I have linked an issue to this PR.
  • I have indicated the change type and relevant package(s).
  • I have proposed a change level by adding one of the version: Major, version: Minor, or version: Patch labels based on the defined Public API.
  • I have updated the documentation accordingly, if necessary.
  • I have added tests to cover my changes, if necessary.
  • All new and existing tests passed.
  • All CI checks are green (linting, build/deploy, etc).
  • At least one core contributor has approved this PR.

@jcalcaben jcalcaben added pkg:pwa-devdocs documentation This pertains to documentation. labels Mar 27, 2019
@vercel
Copy link

vercel bot commented Mar 27, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

@jcalcaben jcalcaben assigned jcalcaben and unassigned jcalcaben Mar 29, 2019
@supernova-at supernova-at self-assigned this Apr 2, 2019
Copy link
Contributor

@supernova-at supernova-at left a comment

Choose a reason for hiding this comment

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

This is great and will really help people!

</html>
```

This template partial closes the `body` and `html` tag for the HTML response.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the partials are overkill for this tutorial.

Since this isn't a tutorial on Mustache, I think the hello-world.mst template could just contain all of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that partials add some complexity for a simple tutorial. However, this pattern is used in the Venia implementation. Readers will undoubtedly draw comparisons between what's in the Venia source and how we're telling them to do something.

title: Adding React
---

This tutorial teaches you how to add webpack to your UPWARD project and use it to create a React application.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this topic can be made much simpler by getting rid of all the "extras" around React.

Since React doesn't actually need JSX you can get rid of the dependency on babel and webpack here.

Check it out:

hello-world.mst

{{> templates/open-document}}

<title>{{ title }}</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js"></script>

{{> templates/open-body}}

<div id="root">Loading...</div>

<script defer src="/app.js"></script>

{{> templates/close-document}}

Notice that the template is pulling in React and React DOM from CDNs. This eliminates the need to install them locally.

app.js

const App = () => {
    return React.createElement(
        'h1',
        null,
        'Hello World from React!'
    );
};

ReactDOM.render(
    React.createElement(App),
    document.getElementById('root')
);

Notice that we aren't using JSX here and instead using the React.createElement function directly. This is what Babel does for you.

The only thing left to do is fix up the UPWARD spec a bit:

status: response.status
headers: response.headers
body: response.body

response:
  resolver: conditional
  when:
      - matches: request.url.pathname
        pattern: '^/?$'
        use: helloWorld
      - matches: request.url.pathname
        pattern: '^/hello-world/?$'
        use: helloWorld
      - matches: request.url.pathname
        pattern: '^/app.js$'
        use: appScript
  default: notFound

helloWorld:
  inline:
      status:
          resolver: inline
          inline: 200
      headers:
          resolver: inline
          inline:
              content-type:
                  resolver: inline
                  inline: 'text/html'
      body:
          resolver: template
          engine: mustache
          template: './templates/hello-world.mst'
          provide:
            title:
              resolver: inline
              inline: 'This is the page title!'
notFound:
    inline:
        status:
            resolver: inline
            inline: 404
        headers:
            resolver: inline
            inline:
                content-type:
                    resolver: inline
                    inline: 'text/string'
        body:
            resolver: inline
            inline: 'Page not found!'

appScript:
  inline:
    status:
      resolver: inline
      inline: 200
    headers:
      resolver: inline
      inline:
        content-type:
          resolver: inline
          inline: 'application/javascript'
    body:
      resolver: file
      file:
        resolver: inline
        inline: './app.js'
      encoding:
        resolver: inline
        inline: 'utf-8'

Since this is an UPWARD tutorial, I think this greatly simplifies the topic and introduces readers to the FileResolver more quickly.

We probably want to include a paragraph about how production code should use webpack and babel but I don't think they need to be introduced in this tutorial.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even though it's billed as an UPWARD tutorial, I also wanted the end result of doing this tutorial to be an environment that the reader can use as a starting point for their PWA Studio projects. This is something the community has been asking us for.

It's true that React doesn't require JSX, but most examples out there use JSX and ES6 syntax. I think it would be a bad experience for developers if we introduce how something is done in the tutorial and promote something very different in the implementation code.

Co-Authored-By: jcalcaben <jcalcaben@users.noreply.github.com>
supernova-at
supernova-at previously approved these changes Apr 4, 2019
Copy link
Contributor

@supernova-at supernova-at left a comment

Choose a reason for hiding this comment

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

Two minor edits that I think will help differentiate the use cases and give instant feedback to someone going through the tutorial.

Take 'em or leave 'em 😄

@jcalcaben jcalcaben merged commit 97d9f2f into develop Apr 4, 2019
@jcalcaben jcalcaben deleted the jimothy/1046_hello-upward-tutorial branch April 4, 2019 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This pertains to documentation. pkg:pwa-devdocs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Documentation] Setting up a simple UPWARD server
3 participants