question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Discussion] AMP Pages with Gatsby

See original GitHub issue

Opening this issue to discuss a functioning (and flexible) solution for generating valid AMP pages. I’m looking for feedback and improvements to the code, as I do believe it can be better. This solution is working on a project that’s using one template to generate thousands of pages.

The biggest hurdle to generating valid AMP pages with Gatsby is removing all the JS bundles that Gatsby automatically inlines in the head during the build process. My solution is to filter these out in componentWillMount in html.js.

  componentWillMount(props) {

  if (process.env.NODE_ENV === `production`) {
    function isJSFile(obj) {
      if ( (typeof obj.key === 'string' || obj.key instanceof String) ) {
        return path.isAbsolute(obj.key) === true
      }
    }

    const ampify = this.props.headComponents.filter((obj, index) => {
      return obj.key !== 'webpack-manifest' && obj.type !== 'script' && !(isJSFile(obj))
    })

    this.props.headComponents = ampify
  }
}

I feel like this solution is still a bit hacky. Given that AMP pages won’t use any JS, it seems like an unnecessary use of cycles to even generate the files in the first place. Personally, I think it would be much cleaner to pass a flag to a build process that instructs Gatsby not to output any JS files.

I’m curious what folks that more familiar with Gatsby internals think. Would a plugin be a more flexible solution? Are there any gotchas, or conerns, you have with this approach?

I’m working on a more indepth write-up to share the full project implementation. Feel free to ask questions in the meantime.

Big thanks in advance to @KyleAMathews for answering my questions on Twitter throughout the development process and for making such a fun, elegant framework.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chiedocommented, Dec 26, 2017

@misscs there are A LOT of different way you can treat AMP pages. In my solution, you can do whatever you want after this. https://github.com/chiedo/gatsby-amp-starter-blog/blob/master/ampify.js#L65

I currently use my solution on 5 websites and it works like a charm. 😃

But my way isn’t the only way. Just a way that is proven and works in production and can be customized. It’s definitely not plug and play… coding is required.

0reactions
KyleAMathewscommented, Sep 8, 2018

Due to the high volume of issues, we’re closing out older ones without recent activity. Please open a new issue if you need help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gatsby and AMP? : r/gatsbyjs - Reddit
Hey guys,. I want to ask/vent about Googles AMP project in regards to Gatsby. I make Gatsby websites for my clients. I love...
Read more >
gatsby-plugin-amp
gatsby -plugin-amp. Formats AMP-specific pages by removing javascript, combining styles and adding boilerplate. Read more about AMP (Accelerated Mobile ...
Read more >
AMP using Gatsby - DEV Community ‍ ‍
I created gatsby plugin (called gatsby-plugin-html2amp) for generating AMP (Accelerated Mobile Pages). I try to explain how to use it.
Read more >
Gatsby Plugin For AMP Pages - faircloth.xyz
A GatsbyJS plugin that creates AMP pages at build time for SEO. ... Gatsby is a fantastic static site generator for React.
Read more >
The great gatsby Lit Chart - The best way to study, teach, and ...
notes about the book the best way to study, teach, and learn about books. the great gats ba back ckgr ground ound info...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found