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

@wishy-gift/noscript

v0.0.14

Published

A small collection of utils for creating web apps that work without javascript

Downloads

1

Readme

@wishy-gift/noscript

Table of Contents

About

A small collection of utils and React components to assist in developing web applications that seamlessly work without JavaScript enabled on the client.

In short, by leveraging the similarities of form events and an event based state container like Redux, we can dispatch and update client/app state on the server side for users without JS enabled, by making all essential actions go through forms.

Instead of directly dispatching actions, we serialize the action type/creator and payload in the form body itself. The helpers in this package is meant to make this task easier.

More details on this can be found in "Using React, Redux and SSR to acommodate users without JavaScript", or this talk from React Advanced 2021 (soon™)

Installation

npm i @wishy-gift/noscript

Important note

For these components to be of any use, you should be listening for POST requests towards your view routes. One of the basic assumptions we make, is that the only reason why someone would do a POST-request to a view route is because they've disabled JS, and an action has been dispatched. This way we know when to update the client state for our users server-side, and re-render the page.

Examples

Components

Form

Will serialize the app state to the DOM, and handle client side state updates by calling preventDefault when the form is submitted, parse the form data, and dispatch the correct action/actionCreator.

Including

import Form from '@wishy-gift/noscript/dist/components/Form';

Props

interface FormProps {
  actionType?: string; // `type` to dispatch
  actionCreator?: string | Function; // function or name of function. see section about actionCreators below
  children: ReactNode;
  className?: string;
  onSubmit?: Function; // optional function to call on submit. NOTE: Actions are dispatched for you
  payloadType?: 'string' | 'object' | 'json'; // default is 'object' which means you can use array notation like payload[foo][bar]
}

Note: You cannot specify both actionType AND actionCreator, but you can also omit passing any of them as props, instead opting to render it to the DOM like this:

<input name="actionType" value={someAction().type} type="hidden" readOnly />

Example

<Form
  className="new-todo-wrapper"
  actionType={addTodo().type}
  onSubmit={resetOnSubmit}
>
  <input
    className="new-todo"
    name="payload[text]"
    placeholder="What needs to be done?"
    autoFocus
  />
  <input type="hidden" name="payload[id]" value={nanoid()} />
</Form>

Button

A simple wrapper for Form with a <button> as child and payloadType="json".

Including

import Button from '@wishy-gift/noscript/dist/components/Button';

Props

interface ButtonProps {
  wrapperClassName?: string; // className to <Form>
  wrapperParams?: object; // spread to <Form>
  className?: string; // className for <button>
  action?: {
    type: string;
    payload?: any; // will be stringifed and rendered to DOM if `payload` isn't provided
  };
  actionCreator?: string | Function; // function or name of function. see section about actionCreators below
  payload?: any; // will be stringifed and rendered to DOM if provided
  children: ReactNode;
  onSubmit?: Function; // optional function to call on submit. NOTE: Actions are dispatched for you
}

Note: As with Form, you cannot specify both action AND actionCreator.

Example

<Button
  className="btn"
  actionCreator={fetchBooks}
  disabled={!nextUrl}
  payload={{
    url: nextUrl,
  }}
>
  Next page
</Button>

Utils

handleServerActions

Given a body and a callback for getting a Redux store given an initial state, will get the store, dispatch any action/actionCreator in body, and return the store and its updated state.

Returns a Promise that resolves with {reduxStore, state}

Including

import handleServerActions from '@wishy-gift/noscript/dist/utils/handleServerActions';

Parameters

type HandleServerActionsParams = {
  data: Data; // one of these must be provided
  rawBody: string; // one of these must be provided
  getReduxStore: (state: any) => ReturnType<typeof configureStore>;
};

type Data = {
  actionType?: string;
  actionCreatorName?: string;
  payloadType: 'string' | 'object' | 'json';
  state?: string;
  payload?: any;
};

Example

const rawBody = req.read().toString();
// const data = qs.parse(rawBody);

const result = await handleServerActions({
  rawBody,
  // data, // if pre-parsed
  getReduxStore: (state) => {
    return getStore(state, isServer);
  },
});

const { reduxStore, state } = result;

actionCreators

Helpers for mapping actionCreators to their typePrefix and vice-versa. Specifically made to be compatible with thunks created with createAsyncThunk. Should be run once, eg. in _app.js for Next.js projects.

Including

import { addActionCreators } from '@wishy-gift/noscript/dist/utils/actionCreators';

Parameters

// Basically the signature of an `AsyncThunk` created with `createAsyncThunk`
type SimpleActionCreator = Function & {
  typePrefix?: string;
};

Example

// _app.js

import { fetchBooks } from './gutenberg/gutenbergSlice';
import { incrementBySurprise } from './counter/counterSlice';

addActionCreators({ fetchBooks, incrementBySurprise });

usePayloadProxy

A hook that returns a Proxy to easier provide the corect array notation to the name attribute of form elements by simply accessing the proxy.

Including

import usePayloadProxy from '@wishy-gift/noscript/dist/hooks/usePayloadProxy';

Parameters

varName?: string; // defaults to 'payload'

Example

const MyInput = () => {
  const payloadProxy = usePayloadProxy('payload'); // 'payload' is default

  return <input name={payloadProxy.foo.bar} value="baz" type="text" />;
};

will render

<input name="payload[foo][bar]" value="baz" type="text" />

License

See LICENSE