<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Posts tagged #file system route api</title><description>All posts tagged with file system route api on queen.raae.codes</description><link>https://queen.raae.codes/tag/file-system-route-api/</link><item><title>📄 3️⃣ ~ Multiple parrot pages for the same node type in Gatsby</title><link>https://queen.raae.codes/2022-03-24-parrot-pages/</link><guid isPermaLink="true">https://queen.raae.codes/2022-03-24-parrot-pages/</guid><description>How can you achieve different page layouts for content with the same content node type but varying content types such as posts, projects, and pages? The…</description><pubDate>Thu, 24 Mar 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;How can you achieve different page layouts for content with the same content node type but varying content types such as posts, projects, and pages?&lt;/p&gt;
&lt;p&gt;The approach covered in yesterday&apos;s email used &lt;a href=&quot;/2022-03-23-page-components/&quot;&gt;multiple page components&lt;/a&gt;; the day before was &lt;a href=&quot;/2022-03-22-page-templates/&quot;&gt;multiple page templates&lt;/a&gt; and today we are covering my current favorite: &lt;em&gt;multiple parrot pages&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If you are not familiar with the term parrot pages, it&apos;s what we call files system routes around the Gatsby islands 🤪&lt;/p&gt;
&lt;h2&gt;Multiple Parrot Pages 🦜&lt;/h2&gt;
&lt;p&gt;The title is a little misleading as we cannot make multiple parrot pages for the same node type. But we&apos;ll use a nice little trick of creating new nodes named after the content type, resulting in the opportunity to make multiple parrot pages:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;src/
├─ pages/
│  ├─ post/
│  │  ├─ {Post.slug}.js
│  ├─ project/
│  │  ├─ {Project.slug}.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We create new nodes by hooking into the &lt;code&gt;onCreateNode&lt;/code&gt; extension point:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// gatsby-node.js
const { createFilePath } = require(&amp;quot;gatsby-source-filesystem&amp;quot;);

exports.onCreateNode = (gatsbyUtils) =&amp;gt; {
  const { actions, node, getNode, reporter, createNodeId } = gatsbyUtils;
  const { createNode } = actions;

  if (node.internal.type === &amp;quot;MarkdownRemark&amp;quot;) {
    const parentFileNode = getNode(node.parent);
    const contentType = parentFileNode?.sourceInstanceName || &amp;quot;&amp;quot;;

    const contentNodeId = createNodeId(`${node.id} &amp;gt;&amp;gt;&amp;gt; ${contentType}`);
    const slug = createFilePath({ node: parentFileNode, getNode });

    createNode({
      id: contentNodeId,
      slug: slug,
      childMarkdownRemark: node.id, // 👈
      internal: {
        contentDigest: node.internal.contentDigest,
        type: contentType,
      },
    });

    reporter.info(`${contentType} created for ${slug}`);
  }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Take note of the childMarkdownRemark key added to the new node! It&apos;s what connects the new node to its content. However, if we query the new nodes at this point, we&apos;ll only get the node id.&lt;/p&gt;
&lt;p&gt;How can we get the full content? By modifying their schema using &lt;code&gt;@link&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// gatsby-node.js

exports.createSchemaCustomization = ({ actions }) =&amp;gt; {
  const { createTypes } = actions;

  const typeDefs = `
    type Post implements Node {
      slug: String
      childMarkdownRemark: MarkdownRemark @link
    }
    type Project implements Node {
      slug: String
      childMarkdownRemark: MarkdownRemark @link
    }
  `;

  createTypes(typeDefs);
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now inside our parrot pages, we can query for the content on childMarkdownRemark:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// {Post.slug}.js

import React from &amp;quot;react&amp;quot;;
import { graphql } from &amp;quot;gatsby&amp;quot;;

const PostPage = ({ data }) =&amp;gt; {
  const { html, frontmatter } = data.post?.childMarkdownRemark;
  const { title } = frontmatter;
  return (
    &amp;lt;main&amp;gt;
      &amp;lt;article&amp;gt;
        &amp;lt;header&amp;gt;
          &amp;lt;h1&amp;gt;{title}&amp;lt;/h1&amp;gt;
        &amp;lt;/header&amp;gt;
        &amp;lt;section dangerouslySetInnerHTML={{ __html: html }} /&amp;gt;
      &amp;lt;/article&amp;gt;
    &amp;lt;/main&amp;gt;
  );
};

export default PostPage;

export const query = graphql`
  query ($id: String!) {
    post(id: { eq: $id }) {
      childMarkdownRemark {
        html
        frontmatter {
          title
        }
      }
    }
  }
`;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Same as the other days, the &lt;code&gt;sourceInstanceName&lt;/code&gt; used comes from configuring your &lt;code&gt;gatsby-source-filesystem&lt;/code&gt; with the &lt;code&gt;name&lt;/code&gt; option.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/content/posts`,
        name: &amp;quot;Post&amp;quot;,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/content/projects`,
        name: &amp;quot;Project&amp;quot;,
      },
    },
  ],
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;How do you solve different page layouts for content with the same content node type but varying content types?&lt;/p&gt;
&lt;p&gt; &lt;br&gt;
All the best,&lt;br&gt;
Queen Raae&lt;/p&gt;
</content:encoded></item><item><title>🦜 📄 ~ Gatsby Parrot Pages</title><link>https://queen.raae.codes/2022-01-13-parrot-pages/</link><guid isPermaLink="true">https://queen.raae.codes/2022-01-13-parrot-pages/</guid><description>In the sharky waters around the Gatsby islands, we call the File System Route API SSG pages for Parrot Pages 🦜 as mentioned in Tuesday&apos;s email about SSG with…</description><pubDate>Thu, 13 Jan 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;In the sharky waters around the Gatsby islands, we call the File System Route API SSG pages for Parrot Pages 🦜 as mentioned in Tuesday&apos;s email about &lt;a href=&quot;/2022-01-11-ssg/&quot;&gt;SSG with Gatsby&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;./parrot-pages.jpeg&quot; alt=&quot;{} = parrots&quot; title=&quot;Drawing of `src/pages/{YouTube.id}.js` with {} as parrots&quot;&gt;&lt;/p&gt;
&lt;p&gt;Making abstract concepts like that come alive is one of Cap&apos;n Ola Vea&apos;s superpowers.&lt;/p&gt;
&lt;p&gt;Hear him present the naming to Ward Peters of Gatsby &lt;a href=&quot;https://youtu.be/TX5XPuHhz9o?t=3265&quot;&gt;in this live stream&lt;/a&gt; all about Parrot Pages.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://youtu.be/TX5XPuHhz9o?t=3265&quot;&gt;&lt;img src=&quot;./yt-screenshot.jpg&quot; alt=&quot;Screengrab of Ola presenting Parrot Pages&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What imagery would you choose for the &lt;a href=&quot;https://www.gatsbyjs.com/docs/reference/routing/file-system-route-api/#creating-client-only-routes&quot;&gt;File System Route API&lt;/a&gt; square bracket (&lt;code&gt;[]&lt;/code&gt;) pages?&lt;/p&gt;
&lt;p&gt;For a demonstration of both Parrot Pages and square bracket (&lt;code&gt;[]&lt;/code&gt;) pages side-by-side, check out the demo for &lt;a href=&quot;https://codesandbox.io/s/demo-ssr-fallback-route-9pteu?file=/README.md&quot;&gt;SSR Fallback routes with Gatsby&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt; &lt;br&gt;
All the best,&lt;br&gt;
Queen Raae&lt;/p&gt;
</content:encoded></item></channel></rss>