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

@react-libraries/mixed-props

v0.0.4

Published

Mixed Client/Server version of getServerSideProps in Next.js

Downloads

4

Readme

@react-libraries/mixed-props

Works similar to getServerSideProps on Next.js.
The difference is that the processing is done on the client side except during SSR, and the props for the page are cached on the client side.

Sample

usage

  • Use useMixedReload to remove the cache and re-run getMixedProps.
    • Clear the cache of the current page and reload.
      • useMixedReload()
    • Delete and reload caches whose head matches /contents.
      • useMixedReload('/contents')
    • Delete all caches and reload
      • useMixedReload(true)
  • If {cache:false} is given as the return value of getMixedProps, the props are no longer cached.

src/pages/_app.tsx

Functions are allocated to each page component from getInitialProps.

import { AppProps } from "next/app";
import { getInitialProps, initMixed } from "@react-libraries/mixed-props";

const App = (props: AppProps) => {
  const { Component } = props;
  const mixedProps = initMixed(props);
  return <Component {...mixedProps} />;
};
App.getInitialProps = getInitialProps;
export default App;

src/pages/index.tsx

By giving getMixedProps to the page component, it behaves in a similar way to getServerSideProps.

import React from "react";
import Link from "next/link";
import { GetMixedProps, useMixedReload } from "@react-libraries/mixed-props";
import { WeatherArea } from "../types/weather";

type Props = { area: WeatherArea; date: string };

const Page = ({ area, date }: Props) => {
  const dispatch = useMixedReload();
  return (
    <>
      <div>
        <button onClick={() => dispatch()}>Reload</button>
      </div>
      <div>Update:{new Date(date).toLocaleString()}</div>
      {area &&
        Object.entries(area.offices).map(([code, { name }]) => (
          <div key={code}>
            <Link href={`/weather/${code}`}>
              <a>{name}</a>
            </Link>
          </div>
        ))}
    </>
  );
};
const getMixedProps: GetMixedProps = async () => {
  const area = (await fetch(
    `https://www.jma.go.jp/bosai/common/const/area.json`
  )
    .then((r) => r.json())
    .catch(() => undefined)) as WeatherArea | undefined;
  return { props: { area, date: new Date().toISOString() } };
  // If cache is not used
  // return { props: { area, date: new Date().toISOString() } ,cache: false };
};
Page.getMixedProps = getMixedProps;
export default Page;

src/pages/weather/[id].tsx

import Link from "next/link";
import React from "react";
import { GetMixedProps, useMixedReload } from "@react-libraries/mixed-props";
import { Weather } from "../../types/weather";

type Props = {
  weather?: Weather;
  date: string;
};

const Page = ({ weather, date }: Props) => {
  const dispatch = useMixedReload();
  return (
    <>
      <div>
        <button onClick={() => dispatch()}>Reload</button>
      </div>
      <div>Update:{new Date(date).toLocaleString()}</div>
      {weather && (
        <>
          <h1>{weather.targetArea}</h1>
          <div>{new Date(weather.reportDatetime).toLocaleString()}</div>
          <div>{weather.headlineText}</div>
          <pre>{weather.text}</pre>
        </>
      )}
      <div>
        <Link href="/">
          <a>Return</a>
        </Link>
      </div>
    </>
  );
};
const getMixedProps: GetMixedProps<Props> = async ({ query }) => {
  const weather = (await fetch(
    `https://www.jma.go.jp/bosai/forecast/data/overview_forecast/${query.id}.json`
  )
    .then((r) => r.json())
    .catch(() => undefined)) as Weather | undefined;
  return { props: { weather, date: new Date().toISOString() } };
};
Page.getMixedProps = getMixedProps;
export default Page;

src/pages/types/weather.ts

export interface Center {
  name: string;
  enName: string;
  officeName?: string;
  children?: string[];
  parent?: string;
  kana?: string;
}
export interface Centers {
  [key: string]: Center;
}
export interface WeatherArea {
  centers: Centers;
  offices: Centers;
  class10s: Centers;
  class15s: Centers;
  class20s: Centers;
}
export interface Weather {
  publishingOffice: string;
  reportDatetime: Date;
  targetArea: string;
  headlineText: string;
  text: string;
}