This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<response pattern="hello, hi, how are you, good morning" id="start"> | |
<text>Hello!</text> | |
<text>How are you doing?</text> | |
<button href="#mood__fine">Fine</button> | |
<button href="#mood__bad">Could be better</button> | |
<button href="http://korytnicki.pl">Interesting website</button> | |
</response> | |
<response id="mood__fine" pattern="good, fine, great"> | |
<text>Great!</text> | |
</response> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Zagadka { | |
constructor(pass) { | |
this.pass = pass; | |
this.letters = []; | |
} | |
literka(letter) { | |
this.letters.push(letter.toLowerCase()); | |
return this.haslo(); | |
} | |
haslo() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import { myModule } from './MyModule'; | |
export function middleware() { | |
MyModule.log('sending a request'); // are you sure it works? | |
axios.get('http:/myservice.com'); // won't work! Depends on library compatibility with Edge Runtime | |
return NextResponse.next(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { origin } = request.nextUrl.clone(); | |
fetch(origin + '/api/myAPI', { | |
... | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handler: NextApiHandler = (request: NextApiRequest, response: NextApiResponse) => { | |
MyModule.log('sending a request'); // yup, no worries | |
axios.get('http://myservice.com'); // fine, why not! | |
return response.status(200).json({ ok: true }); | |
}; | |
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// works, get replaced in build time | |
const someVar = process.env.MY_VARIABLE; | |
// doesn't work! How should Next know how to replace it in build time? | |
function getVariable(name) { | |
return process.env[name]; | |
} | |
const someVar = getVariable(MY_VARIABLE); | |
// try this! | |
console.log(process.env); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextResponse } from 'next/server'; | |
import type { NextRequest } from 'next/server'; | |
import { anotherMiddleware } from './middlewares/anotherMiddleware'; | |
import { someMiddleware } from './middlewares/someMiddleware'; | |
type middlewareFunction = (request: NextRequest, response: NextResponse) => NextResponse | Promise<NextResponse>; | |
const MIDDLEWARES: middlewareFunction[] = [someMiddleware, anotherMiddleware]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function middleware(request: NextRequest) { | |
const response = NextResponse.next(); | |
for await (const middlewareFunction of MIDDLEWARES) { | |
const middlewareResponse = await middlewareFunction(request, response); | |
if (isRedirecting(middlewareResponse)) { | |
return middlewareResponse; | |
} | |
if (isRewriting(middlewareResponse)) { | |
return middlewareResponse; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const config = { | |
matcher: ['/about/:path*', '/dashboard/:path*'], | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// const { pathname } = request.nextUrl; | |
function isPage(pathname: string): boolean { | |
return !(pathname.includes('.') || pathname.startsWith('/api') || pathname.includes('_next')); | |
} | |
// and consequently, you can work out how to identify a file a or an API route with this snippet! |
OlderNewer