Skip to content

Commit

Permalink
chore(next-example): add edge example and publish to vercel
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Feb 28, 2024
1 parent 9944d05 commit 18ef00c
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 45 deletions.
39 changes: 6 additions & 33 deletions next-example/README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
# Example for next and vercel edge functions

## Getting Started
Make sure it's work in vercel enviroment.

First, run the development server:
Links:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
https://xior-next-example.vercel.app/
https://xior-next-example.vercel.app/api/hello-edge
https://xior-next-example.vercel.app/api/hello-node
9 changes: 9 additions & 0 deletions next-example/app/api/hello-edge/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { http } from '@/app/http';

export const runtime = 'edge';
export const dynamic = 'force-dynamic'; // static by default, unless reading the request

export async function GET(request: Request) {
const { data } = await http.get('https://google.com');
return new Response(`Hello from ${process.env.VERCEL_REGION} ${data.length}`);
}
9 changes: 9 additions & 0 deletions next-example/app/api/hello-node/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { http } from '@/app/http';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic'; // static by default, unless reading the request

export async function GET(request: Request) {
const { data } = await http.get('https://google.com');
return new Response(`Hello from ${process.env.VERCEL_REGION} ${data.length}`);
}
12 changes: 12 additions & 0 deletions next-example/app/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import xior from 'xior';
import cachePlugin from 'xior/plugins/cache';
import errorRetryPlugin from 'xior/plugins/error-retry';
import uploadDownloadProgressPlugin from 'xior/plugins/progress';
import throttlePlugin from 'xior/plugins/throttle';

export const http = xior.create();

http.plugins.use(errorRetryPlugin());
http.plugins.use(throttlePlugin());
http.plugins.use(cachePlugin());
http.plugins.use(uploadDownloadProgressPlugin());
5 changes: 2 additions & 3 deletions next-example/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import Image from 'next/image';

import http from './xior-instance';

console.log(http);

export default function Home() {
export default async function Home() {
await http.get('https://google.com');
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
Expand Down
9 changes: 1 addition & 8 deletions next-example/app/xior-instance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
// @ts-ignore
import stringify from 'qs/lib/stringify';
import xior, { merge, delay, buildSortedURL, encodeParams } from 'xior';
import cachePlugin from 'xior/plugins/cache';
import errorRetryPlugin from 'xior/plugins/error-retry';
import uploadDownloadProgressPlugin from 'xior/plugins/progress';
import throttlePlugin from 'xior/plugins/throttle';

// console.log(merge, delay, buildSortedURL);

const instance = xior.create({
encode: (params: Record<string, any>) => stringify(params),
});
const instance = xior.create({});
instance.plugins.use(errorRetryPlugin({}));
instance.plugins.use(cachePlugin({}));
instance.plugins.use(throttlePlugin({}));
Expand Down
28 changes: 28 additions & 0 deletions next-example/middleware.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import xior from 'xior';
import cachePlugin from 'xior/plugins/cache';
import errorRetryPlugin from 'xior/plugins/error-retry';
import uploadDownloadProgressPlugin from 'xior/plugins/progress';
import throttlePlugin from 'xior/plugins/throttle';

export const http = xior.create();

http.plugins.use(errorRetryPlugin());
http.plugins.use(throttlePlugin());
http.plugins.use(cachePlugin());
http.plugins.use(uploadDownloadProgressPlugin());

import { NextResponse } from 'next/server';

export async function middleware(request) {
const { data } = await http.get('https://google.com'); // for test
console.log(data.length);
}

export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
// Optional: only run on root (/) URL
// '/'
],
};
2 changes: 1 addition & 1 deletion next-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"react": "^18",
"react-dom": "^18",
"next": "14.1.0",
"xior": "workspace:*"
"xior": "^0.0.7"
},
"devDependencies": {
"typescript": "^5",
Expand Down
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 18ef00c

Please sign in to comment.