+ - 0:00:00
Notes for current slide
Notes for next slide
Explaining UPWARD properly - Ahmedabad Magento Meetup

Explaining UPWARD properly

a Magento PWA Studio saga

1 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
  • Founder of Yireo
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
  • Founder of Yireo
  • Extension developer
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
  • Founder of Yireo
  • Extension developer
  • Trainer in Magento 2, React, Vue
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
  • Founder of Yireo
  • Extension developer
  • Trainer in Magento 2, React, Vue
  • Creator of MageTestFest & Reacticon
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
  • Founder of Yireo
  • Extension developer
  • Trainer in Magento 2, React, Vue
  • Creator of MageTestFest & Reacticon
  • Magento Master 2017 & 2018 & 2019 Mover
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

  • From the Netherlands
  • Founder of Yireo
  • Extension developer
  • Trainer in Magento 2, React, Vue
  • Creator of MageTestFest & Reacticon
  • Magento Master 2017 & 2018 & 2019 Mover
  • Member of ExtDN.org
2 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

React

  • Create React App / Webpack
  • React source
  • Redux state management
  • Apollo client for GraphQL
3 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

PWA Studio

  • Buildpack
  • Venia Concept theme
  • Venia UI component library
  • Peregrine
  • UPWARD
4 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

PWA Studio

  • Buildpack
    • Wrapper for Webpack
  • Venia Concept theme
    • Template for your own React source
  • Venia UI component library
    • Redux state management
    • Apollo client for GraphQL
  • Peregrine
    • Collection of hooks and more
  • UPWARD
5 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

So ... UPWARD

6 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Unified Progressive Web App Response Definition

7 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD

  • Proxy between PWA & Magento
    • Or: A middle tier service layer between browser & server
    • Unified Progressive Web App Response Definition
  • Mapping of properties, functions & data
  • No state
  • Defined in YAML
8 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

But what is doing UPWARD?

9 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Benefits & downsides

10 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Benefits of UPWARD

  • Local SSL certificate
    • Ability to work with serviceworker
  • Local domain
  • Easy deployment to non-Node Magento hosting
    • Using the UPWARD Magento/PHP module
11 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Downsides of UPWARD

  • Potentially slower
    • Resources are routed through UPWARD (including static files)
    • Potential fixes using webserver configuration
  • Complexer to understand
12 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

But what is doing UPWARD?

13 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Running UPWARD

14 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD connectors

  • NodeJS (for instance React or Vue)
  • Magento UPWARD module
  • Standalone PHP (for instance, Laravel or Symfony)
15 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Running UPWARD

  • Development
    • Part of Buildpack
  • Production
    • Node / UPWARD standalone
    • Node / Express
    • PHP / Magento module
    • PHP / something else
16 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Development

yarn create @magento/pwa
yarn watch
17 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Production: Node standalone

Install globally:

npm install -g @magento/upward-js

And then run:

UPWARD_JS_UPWARD_PATH=/abc/upward.yml \
UPWARD_JS_BIND_LOCAL=1 \
UPWARD_JS_LOG_URL=1 \
upward-js-server

You can guarantee that upward-js-server remains running by using PM2

18 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Production: Node in Express

Create a new project (yarn init) and add to it:

yarn add @magento/upward-js express
19 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Create an Express server

File example-express/example-server.js:

const { createUpwardServer } = require('@magento/upward-js');
async function serve() {
await createUpwardServer({
port: 8000,
bindLocal: true,
logUrl: true,
upwardPath: '/abc/upward.yml'
});
}
serve();

Note that there only needs to be 1 file in this new repository

20 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Running the Express server

Usage:

node example-server.js

You can guarantee that example-server.js remains running by using PM2

21 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Production: Magento module

  • Install and enable the module
  • Configure the specification
22 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Install and enable

  • composer require magento/module-upward-connector
  • bin/magento module:enable Magento_UpwardConnector
23 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Configuring things

  • Stores > Configuration > General > Web > UPWARD PWA Configuration
    • Example config: /path/to/pwastudio/packages/venia-concept/dist/upward.yml
  • Make sure to build the PWA first (yarn build) and deploy it to production
24 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

But what is doing UPWARD?

25 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD YAML

26 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Which YAML file?

  • Use the YAML file distributed by Venia
    • dist/upward.yml
  • Custom create your own YAML
    • upward-hello.yml
27 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

upward-hello.yml

status: 200
headers:
inline:
content-type:
inline: text/html
body:
inline: 'Hello World'

See: https://itnext.io/magento-pwa-studio-what-is-upward-acf450fbee3e

28 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Rendering a CMS Page

resource:
when:
- matches: urlResolver.type
pattern: 'CMS_PAGE'
use:
inline:
model: cmsPage
name: cmsPage.title
entityTypeName:
inline: "Page"
template: '../venia-ui/templates/cmspage-shell.mst'

See https://www.czettner.com/2020/02/14/magento-2-pwa-studio-ssr.html

29 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Mustache templates (1)

  • open-document.mst
    • Beginning of HTML response, opening of <head>
  • open-body.mst
    • Closing of <head>, opening of <body>
  • close-document.mst
    • Closing of <body>
30 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Mustache templates (2)

Usage in other file (example.mst):

{{> templates/open-document}}
<title>{{ title }}</title>
{{> templates/open-body}}
<b>Hello Template World!</b>
{{> templates/close-document}}
31 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Mustache templates (3)

File venia-ui/templates/cmspage-shell.mst:

{{> templates/open-document}}
{{#model}}
<title>{{title}} - My awesome site</title>
<meta name="title" content="{{meta_title}}">
<meta name="keywords" content="{{meta_keywords}}">
<meta name="description" content="{{meta_description}}">
{{/model}}
{{> templates/open-body }}
{{> templates/default-initial-contents }}
{{> templates/seed-bundles}}
{{> templates/close-document}}
32 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Mustache templates (4)

  • venia-ui/templates/generic-shell.mst
  • venia-ui/templates/cmspage-shell.mst
  • venia-ui/templates/product-shell.mst
  • venia-ui/templates/category-shell.mst
33 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

FileResolver

Example snipper from UPWARD YAML:

query:
resolver: file
file:
resolver: inline
inline: './productDetail.graphql'
encoding:
resolver: inline
inline: 'utf-8'

The property query is filled with the contents of the GraphQL file

34 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD Resolvers (1)

  • InlineResolver
    • Content is part of YAML
  • FileResolver
    • Content is read from file
  • ServiceResolver
    • Content is derived from GraphQL backend
  • TemplateResolver
    • Content is read from a Mustache template file
35 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD Resolvers (2)

  • ConditionalResolver
    • Choices being made based on when and matches
  • ProxyResolver
    • Proxy between PWA and backend (like REST)
  • DirectoryResolver
    • Proxying to local filesystem (like with assets)
  • UrlResolver
    • Building URLs
36 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

So in short, UPWARD uses its own specifications in YAML to jump from one part to another to do what ...?

37 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

But what is doing UPWARD?

38 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD and CSS requests

  • PWA in browser requests a CSS file
  • Request is forwarded from Nginx to UPWARD
  • Request is handled by UPWARD
  • Response is sent back from UPWARD to Nginx
  • Response is sent back from Nginx to browser

Remember that UPWARD has no state, so no caching

39 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD and JS requests

  • PWA in browser requests a JS file
  • Request is forwarded from Nginx to UPWARD
  • Request is handled by UPWARD
  • Response is sent back from UPWARD to Nginx
  • Response is sent back from Nginx to browser

Remember that UPWARD has no state, so no caching

40 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD and static requests

  • PWA in browser requests any static file
  • Request is forwarded from Nginx to UPWARD
  • Request is handled by UPWARD
  • Response is sent back from UPWARD to Nginx
  • Response is sent back from Nginx to browser

Remember that UPWARD has no state, so no caching

41 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

If a proxy in between the browser and static files is not caching anything, performance is better if you remove the proxy.

42 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

In short: Do not use UPWARD for static files, but use Nginx instead

43 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD & GraphQL requests

  • PWA in browser requests a JS file
  • Request is forwarded from Nginx to UPWARD
  • Request is handled by UPWARD
  • Response is sent back from UPWARD to Nginx
  • Response is sent back from Nginx to browser

Remember that UPWARD has no state, so no caching

44 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

If a proxy in between the browser and GraphQL is not caching anything, performance is better if you remove the proxy.

45 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

In short: Do not use UPWARD for GraphQL, but use Magento directly

46 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Venia without UPWARD

  • Edit index.js
    • const apiBase = new URL('https://m2.vega.yr/graphql').toString();
  • Cleanup
    • Remove server.js, upward.yml
47 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

My personal opinion: Do not use UPWARD in production, but use Nginx for this instead

48 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Unless you want to do SSR (Server Side Rendering) with UPWARD ...

49 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

but there are other better solutions maycbe for that.

50 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Is it useful in development?

51 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

UPWARD in development

  • Buildpack creates a dynamic hostname + SSL
  • The same domain name is serving all requests
  • No issues with CORS
    • Cross-Origin Resource Sharing
52 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Other CORS solutions

53 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

My personal opinion: You do not even need UPWARD in development, if you are solving the CORS problem differently

54 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Disclaimer: This is just me speaking

55 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Disclaimer: I'm not going to be a Magento Master 2020>

56 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Thanks

57 of 57
Explaining UPWARD properly - Ahmedabad Magento Meetup

Jisse Reitsma

2 of 57
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow