11.11.3 • Published 3 months ago

@uxf/router v11.11.3

Weekly downloads
38
License
ISC
Repository
gitlab
Last release
3 months ago

@uxf/router

Installation

yarn add @uxf/router
  • Create routes directory
  • Create routes.ts and index.ts inside routes directory:
// routes/routes.ts

import { createRouter } from "@uxf/router";

interface RouteList {
    index: null;
    "admin/index": { param1?: number };
    "blog/detail": { id: number };
}

export default createRouter<RouteList>({
    index: "/",
    "admin/index": "/admin",
    "blog/detail": "/blog/[id]",
});
// routes/index.ts

import router, { RouteList } from "./routes";
import { UxfGetServerSideProps, UxfGetStaticProps } from "@uxf/router";
import { PreviewData as NextPreviewData } from "next/types";

export const { route, routeToUrl, sitemapGenerator, useQueryParams } = router;

export type GetStaticProps<
    Route extends keyof RouteList,
    Props extends { [key: string]: any } = { [key: string]: any },
    PreviewData extends NextPreviewData = NextPreviewData,
    > = UxfGetStaticProps<RouteList, Route, Props, PreviewData>;

export type GetServerSideProps<
    Route extends keyof RouteList,
    Props extends { [key: string]: any } = { [key: string]: any },
    PreviewData extends NextPreviewData = NextPreviewData,
    > = UxfGetServerSideProps<RouteList, Route, Props, PreviewData>;

Add configuration to tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@app-routes": [
        "routes"
      ]
    }
  }
}

useQueryParams

import {useQueryParams} from "@app-routes";
import {queryParamToNumber} from "./helper";

const params = useQueryParams<"route-name">()

const id = queryParamToNumber(params.id);

Next Link

// pages/index.js

import Link from "next/link";
import { route } from "@app-routes";

export default () => (
    <Link href={route("blog/detail", { id: 12 })}>
      Hello world
    </Link>
)

GetStaticProps

import { GetStaticProps } from "@app-routes";
import { queryParamToNumber } from "@uxf/router";

export const getStaticProps: GetStaticProps<"blog/detail"> = (context) => {
    const id = queryParamToNumber(context.params?.id); // context.params is of type { id: number } | undefined
}

GetServerSideProps

import { GetServerSideProps } from "@app-routes";
import { queryParamToNumber } from "@uxf/router";

export const getServerSideProps: GetServerSideProps<"blog/detail"> = (context) => {
    const id = queryParamToNumber(context.params?.id); // context.params is of type { id: number } | undefined
}

Sitemap

Create sitemap items

// sitemap-items.ts in @app-routes

import { createSitemapGenerator, routeToUrl } from "@app-routes";

export const sitemapItems = createSitemapGenerator({baseUrl: 'http://localhost:3000', defaultPriority: 1})
    .add("index", async (route) => ({ loc: routeToUrl(route) }))
    .add("blog/detail", async (route) => [
        { loc: routeToUrl(route, {id: 1}), priority: 2 },
        { loc: routeToUrl(route, {id: 2}), priority: 2 },
    ])
    .skip("admin/index")
    .exhaustive();

sitemap.xml

// pages/sitemap.xml.tsx

import React from "react";
import { NextPage } from "next";
import { sitemapItems } from "@app-routes";

const Page: NextPage = () => null;

Page.getInitialProps = async (ctx) => {
    if (ctx.res) {
        ctx.res.setHeader("Content-Type", "text/xml");
        ctx.res.write(await sitemapItems.toXml());
        ctx.res.end();
    }

    return {};
};

export default Page;

sitemap.json

// pages/sitemap.json.tsx

import React from "react";
import { NextPage } from "next";
import { sitemapItems } from "@app-routes";

const Page: NextPage = () => null;

Page.getInitialProps = async (ctx) => {
    if (ctx.res) {
        ctx.res.setHeader("Content-Type", "text/json");
        ctx.res.write(await sitemapItems.toJson());
        ctx.res.end();
    }

    return {};
};

export default Page;
11.11.3

3 months ago

11.10.0

3 months ago

10.0.0-beta.80

8 months ago

10.0.0

7 months ago

10.0.0-beta.17

11 months ago

10.0.0-beta.18

11 months ago

10.0.0-beta.15

11 months ago

10.0.0-beta.16

11 months ago

10.0.0-beta.3

11 months ago

3.0.0-beta.1

1 year ago

2.2.0-beta.1

1 year ago

2.2.0-beta.2

1 year ago

3.0.0-beta.3

1 year ago

3.0.0-beta.2

1 year ago

3.0.0-beta.5

1 year ago

3.0.0-beta.4

1 year ago

2.1.2

1 year ago

2.1.1

2 years ago

1.7.1

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0-beta.1

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago