npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@axistaylor/wpgraphql-next-content-renderer

v0.0.1

Published

Provides components and tooling to render WordPress page content on a 1:1 scale

Readme

WPGraphQL Next Content Renderer

A series of tools and React components for rendering WordPress content pages in Next.js

Usage

  1. Install with npm install @axistaylor/wpgraphql-next-content-renderer
  2. Update your next.config.mjs file:
import { withWCR } from '@axistaylor/wpgraphql-next-content-renderer/withWCR';

/**
 * @type {import('next').NextConfig}
 */
const nextConfig = {
  env: {
    ...
  }
};

const wpDomain = 'example.com';
const wpProtocol = 'https';

export default withWCR(nextConfig, {
  wpDomain,
  wpProtocol,
  wpHomeUrl: `${wpProtocol}://${wpDomain}`,
  wpSiteUrl: `${wpProtocol}://${wpDomain}/wp`, // <-- `/wp` is typically add for some non-traditional WordPress setups like WP Bedrock.
  frontendDomain: 'localhost:3000',
  frontendProtocol: 'http',
});
  1. Add a middleware.ts file:
export const middleware = async (request: NextRequest) => {
  return await proxyByWCR(request);
}

export const config = {
  matcher: [
      '/api/wp',
      '/api/wc',
      '/api/wp-internal-assets/:path*',
      '/api/wp-assets/:path*',
      '/api/wp-json/:path*',
  ],
}
  1. Now you're to query for and render the content pages:
// ./lib/utils.ts
async function fetchContentAndScripts(uri: string) {
  const response = await fetch(process.env.GRAPHQL_ENDPOINT as  string, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
      query: `query ($uri: String!) {
        nodeByUri(uri: $uri) {
          ... on ContentNode {
            enqueuedStylesheets(first: 500) {
              nodes {
                handle
                src
                version
                after
                before
              }
            }
            enqueuedScripts(first: 500) {
              nodes {
                handle
                src
                strategy
                version
                after
                group
                location
                before
                extraData
              }
            }
          }
          ... on Page {
            content
          }
          ... on Post {
            content
          }
        }
      }`,
      variables: { uri }
    }),
    cache: 'no-store',
  });

  const { data } = await response.json();
  const node = data?.nodeByUri;

  if (!node) {
    return {
      content: '',
      scripts: [],
      stylesheets: [],
    };
  }

  const content = node.content;
  const scripts = node.enqueuedScripts.nodes;
  const stylesheets = node.enqueuedStylesheets.nodes;

  return {
    content,
    scripts,
    stylesheets,
  }
}
// ./components/Styles.tsx
'use client';

import { RenderStylesheets, EnqueuedStylesheet } from   "@axistaylor/wpgraphql-next-content-renderer/client";

export default function Styles({ stylesheets }: { stylesheets:   EnqueuedStylesheet[] }) {
  return <RenderStylesheets stylesheets={stylesheets} />;
}
// ./components/Scripts.tsx
import type { PropsWithChildren } from 'react';
import { RenderScripts, EnqueuedScript } from "@axistaylor/  wpgraphql-next-content-renderer";


export interface ScriptsProps {
  scripts: EnqueuedScript[];
  type?: 'header' | 'footer';
}

export default function Scripts({ scripts, type, children }:   PropsWithChildren<ScriptsProps>) {
  return <RenderScripts scripts={scripts} type={type}>{children}</  RenderScripts>;
}
// ./app/page.tsx
import { Content } from '@axistaylor/wpgraphql-next-content-renderer';

import { fetchContentAndScripts } from '@/lib/utils';
import Styles from "@/components/Styles";
import Scripts from "@/components/Scripts";

export default async function Home() {
  const { content, stylesheets, scripts } = await fetchContentAndScripts('/sample-page');

  return (
    <>
      <Styles stylesheets={stylesheets} />
      <Scripts scripts={scripts}>
        <div id="main-content" className="min-h-screen px-4 pt-4">
          <Content content={content} />
        </div>
      </Scripts>
    </>
  );
}

Optimization Recommendation

Some wordpress content pages use a lot of dynamic inline scripts and styles and require the proxyByWCR middleware function to work as intended. This has the added effect of making the route dynamic and incapable of being statically generated. To mitigate the potential performance hit, isolate the wordpress content pages to a route group with it's own dedicated root layout. Read more it here.