Skip to content

window is not defined: AppProvider component seems not SSR-ready for embedded apps #372

@sbstnmsch-zz

Description

@sbstnmsch-zz

Issue summary

I am building a Shopify app using Polaris 2.3.1, Typescript and Next.js, which server-side renders (SSR). Everything is working smooth while not running embedded in the admin.

Running server-side rendered in embedded mode results in a server error: window is not defined

Expected behavior

Polaris should be able to run embedded in admin even if server rendered.

Actual behavior

Crashes server with 500: window is not defined when running embedded in admin.

Steps to reproduce the problem

Starting a SSR app in the admin with apiKey and shopOrigin passed to AppProvider results in:

{ ReferenceError: window is not defined
  at checkFrameRedirect (.../node_modules/@shopify/polaris/index.js:1098:5)

Yikes. There is no window object on the server.

  ...
  
  static async getInitialProps(ctx) {
    return { ...ctx.req.shopify };
  }

  constructor(props) {
    super(props);
  }

  render() {
    return (
      <AppProvider
        apiKey={this.props.apiKey}
        shopOrigin={`https://${this.props.shop}.myshopify.com`}
      >
        <Page>
         ....
    }

Specifications

  • Are you using the React components? (Y/N): Y
  • Polaris version number: 2.3.1
  • Browser: Chrome Canary
  • Device: MacBook
  • Operating System: MacOS 10.13.5

Thoughts

A potential fix could be to move the inter-frame-messaging and redirection setup to a later react live-cycle which runs in browser, not server.

Metadata

Metadata

Assignees

Labels

BugSomething is broken and not working as intended in the system.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions