{"id":14957490,"url":"https://github.com/ryanhefner/next-utils","last_synced_at":"2025-07-19T17:14:17.145Z","repository":{"id":35101153,"uuid":"204042109","full_name":"ryanhefner/next-utils","owner":"ryanhefner","description":"🧰 Handy utilities for building React components that render as nice server-side as they do on the client.","archived":false,"fork":false,"pushed_at":"2023-03-04T04:39:32.000Z","size":1503,"stargazers_count":2,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-14T11:05:47.844Z","etag":null,"topics":["nextjs","react","utilities"],"latest_commit_sha":null,"homepage":"https://www.pkgstats.com/pkg:next-utils","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryanhefner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ryanhefner","patreon":"ryanhefner","open_collective":"ryanhefner","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-08-23T17:27:23.000Z","updated_at":"2024-11-23T19:52:48.000Z","dependencies_parsed_at":"2024-06-21T13:03:52.647Z","dependency_job_id":"350808af-713c-4e85-90a3-14bae068ec26","html_url":"https://github.com/ryanhefner/next-utils","commit_stats":{"total_commits":31,"total_committers":2,"mean_commits":15.5,"dds":0.06451612903225812,"last_synced_commit":"404d373339a4e20455c9f9f96f91b58f85e9305b"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanhefner%2Fnext-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanhefner%2Fnext-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanhefner%2Fnext-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanhefner%2Fnext-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanhefner","download_url":"https://codeload.github.com/ryanhefner/next-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234883314,"owners_count":18901366,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nextjs","react","utilities"],"created_at":"2024-09-24T13:14:58.646Z","updated_at":"2025-01-21T02:08:13.412Z","avatar_url":"https://github.com/ryanhefner.png","language":"JavaScript","funding_links":["https://github.com/sponsors/ryanhefner","https://patreon.com/ryanhefner","https://opencollective.com/ryanhefner"],"categories":[],"sub_categories":[],"readme":"# next-utils\n\n[![npm](https://img.shields.io/npm/v/next-utils?style=flat-square)](https://www.pkgstats.com/pkg:next-utils)\n[![NPM](https://img.shields.io/npm/l/next-utils?style=flat-square)](LICENSE)\n[![npm](https://img.shields.io/npm/dt/next-utils?style=flat-square)](https://www.pkgstats.com/pkg:next-utils)\n\nHandy utilities for building React components that render as nice server-side\nas they do on the client.\n\n## Install\n\nVia [npm](https://npmjs.com/package/next-utils)\n\n```sh\nnpm install --save next-utils\n```\n\nVia [Yarn](https://yarn.fyi/next-utils)\n\n```sh\nyarn add next-utils\n```\n\n## How to use\n\nAfter building a few packages that all handled server-side requests, two common\nfunctions and classes emerged that were almost identical for them all,\n`getDataFromTree` and `RenderPromises`.\n\nFor examples of how these can be used, please reference the [repos that are\nusing these](#used-by)\n\n### `getDataFromTree`\n\nUsed to render a React tree server side and expose the `renderPromises` method\nvia a Provider to allow for children to register themselves and resolve all\nrequests initiated by child components.\n\n### `RenderPromises`\n\nManages and resolves query instances that have registered themselves. Relies on\nall registered instances to have a public `fetchData` method exposed that is\nresponsible registering with the context provided `renderPromises` example.\n\n```js\nimport React, { Component } from 'react';\n\nclass RequestComponent extends Component {\n  constructor(props) {\n    super(props);\n\n    this.state = {\n      data: null,\n      error: null,\n      loading: false,\n      fetched: false,\n    };\n  }\n\n  ...\n\n  async fetchData() {\n    return new Promise((resolve, reject) =\u003e {\n      const {\n        context,\n        url,\n        options,\n        skip,\n      } = this.props;\n\n      try {\n        if (skip) {\n          return resolve(null);\n        }\n\n        const cacheKey = JSON.stringify(this.props);\n\n        if (context.cache \u0026\u0026 context.cache.has(cacheKey)) {\n          return resolve(context.cache.read(cacheKey));\n        }\n\n        const request = fetch(url, options);\n\n        if (context.cache \u0026\u0026 !context.renderPromises) {\n          context.cache.write(cacheKey, request);\n        }\n\n        const response = await request;\n\n        if (context.renderPromises) {\n          context.renderPromises.registerSSRObservable(this, response);\n        }\n\n        return resolve(response);\n      }\n      catch (error) {\n        return rejectt(error);\n      }\n    });\n  }\n\n  ...\n\n  getQueryResult() {\n    return this.state;\n  }\n\n  render() {\n    const {\n      children,\n      context,\n    } = this.props;\n\n    const finish = () =\u003e children(this.getQueryResult());\n\n    if (context \u0026\u0026 context.renderPromises) {\n      return context.renderPromises.addQueryPromise(this, finish);\n    }\n\n    return finish();\n  }\n}\n```\n\n## Other Handy Utils\n\nBecause I got tired of repeating myself with these across multiple projects.\n\n* `isClient` - Basically just, `typeof window !== 'undefined'`\n* `isServer` - And, the inverse, `typeof window === 'undefined'`\n\n```js\nimport { useEffect } from 'react'\nimport { isServer } from 'next-utils'\n\nuseEffect(() =\u003e {\n  if (isServer()) return\n\n  ...do client-side only stuff...\n})\n```\n\n## Used by\n\n* [`next-contentful`](https://github.com/ryanhefner/next-contentful)\n* [`next-request-block`](https://github.com/ryanhefner/next-request-block)\n* [`next-prismic`](https://github.com/ryanhefner/next-prismic)\n\n## License\n\n[MIT](LICENSE) © [Ryan Hefner](https://www.ryanhefner.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanhefner%2Fnext-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanhefner%2Fnext-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanhefner%2Fnext-utils/lists"}