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

[Question] Just use this library as a reference to Amp with Next.js and not adding the SSR methodology that the ampreact shows. #70

Open
MontoyaAndres opened this issue Jan 9, 2020 · 1 comment

Comments

@MontoyaAndres
Copy link

I'm just using react-amphtml to transform all the tags that amp has to Components. I'm not implementing the logic that the ampreact example has. In that case, I'm using the config that with-styled-components has. More specifically, like this:

import Document, {
  DocumentContext,
  Html,
  Head,
  Main,
  NextScript
} from "next/document";
import { ServerStyleSheet } from "styled-components";

export default class MyDocument extends Document {
  static async getInitialProps(ctx: DocumentContext) {
    const sheet = new ServerStyleSheet();
    const originalRenderPage = ctx.renderPage;

    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
        });

      const initialProps = await Document.getInitialProps(ctx);
      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        )
      };
    } finally {
      sheet.seal();
    }
  }

  render() {
    return (
      <Html>
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

So, my question is, I'm just using react-amphtml to use the amp tags as components, something like this:

import React from "react";
import styled from "styled-components";
import * as Amp from "react-amphtml";

const NavBar = styled.nav`
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5em 2em;
  position: sticky;
  z-index: 1;
`;

const MenuIcon = styled(Amp.AmpImg)`
  &:hover {
    cursor: pointer;
  }

  @media (min-width: 768px) {
    display: none;
  }
`;

export const Layout = ({ children }) => {
  return (
    <>
      <NavBar>
        <Amp.AmpImg
          specName="default"
          alt="logo"
          width={60}
          height={30}
          src="/Icons/Diey_Logo.svg"
        />
        <MenuIcon
          specName="default"
          alt="logo"
          width={30}
          height={30}
          src="/Icons/menu.svg"
        />
      </NavBar>
      {children}
    </>
  );
};

Believing that Next.js will care all about SSR with AMP. Is this a good practice?

@dfrankland
Copy link
Owner

From what I can tell from your code, your AMP pages won't work because you aren't adding the script tags for AMP to load. Most of the logic in ampreact is there to help load the boilerplate required for AMP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants