{"id":51691502,"url":"https://github.com/devjubayr/middleware-nextjs","last_synced_at":"2026-07-16T02:34:31.769Z","repository":{"id":322315306,"uuid":"1088873258","full_name":"devjubayr/middleware-nextjs","owner":"devjubayr","description":null,"archived":false,"fork":false,"pushed_at":"2025-11-03T20:39:44.000Z","size":194,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-07-16T02:34:27.719Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devjubayr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-11-03T15:18:08.000Z","updated_at":"2025-11-03T20:39:47.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/devjubayr/middleware-nextjs","commit_stats":null,"previous_names":["crosbow/middleware-nextjs","devjubayr/middleware-nextjs"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/devjubayr/middleware-nextjs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devjubayr%2Fmiddleware-nextjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devjubayr%2Fmiddleware-nextjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devjubayr%2Fmiddleware-nextjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devjubayr%2Fmiddleware-nextjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devjubayr","download_url":"https://codeload.github.com/devjubayr/middleware-nextjs/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devjubayr%2Fmiddleware-nextjs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35528482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-16T02:00:06.687Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2026-07-16T02:34:26.930Z","updated_at":"2026-07-16T02:34:31.762Z","avatar_url":"https://github.com/devjubayr.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Middleware\n\nMiddleware allow to run some login before a request complete.\n\n---\n\n### Send a response from middleware\n\n```javascript\nimport { NextResponse } from \"next/server\";\n\nexport const middleware = (request) =\u003e {\n  // Use: NextResponse  object from nextjs\n  return NextResponse.json({\n    message: \"Hello world\",\n  });\n};\n```\n\n---\n\n### Allow to access next data\n\n```javascript\nimport { NextResponse } from \"next/server\";\n\nexport const middleware = (request) =\u003e {\n  return NextResponse.next();\n};\n```\n\n---\n\n### Redirect to another route\n\nRedirect from middleware is different from \"redirect\" function. We usually use following way to redirect user.\n\n```javascript\nconst { NextResponse } = require(\"next/server\");\n\nexport const middleware = (request) =\u003e {\n  return NextResponse.redirect(new URL(\"/\", request.url));\n};\n\n// This code introduced but, when I try to access \"/\" route nextjs try to get this path and its occur a infinity loop.\n```\n\nCorrected code:\n\n```javascript\nimport { NextResponse } from \"next/server\";\n\nexport const middleware = (request) =\u003e {\n  // Add condition\n\n  if (request.url.includes(\"test\"))\n    return NextResponse.redirect(new URL(\"/\", request.url));\n\n  return NextResponse.next();\n};\n\n// By the way, We can use config instead the condition\n\nexport const config = {\n  matcher: [\"/test\", \"/about\", \"/dashboard/:path*\"],\n};\n```\n\n### Setting cookies\n\n```javascript\nimport { NextResponse } from \"next/server\";\n\nexport const middleware = (request) =\u003e {\n  //  Get cookie from request\n  const cookie = request.cookies.get(\"nextjs\"); // .getAll(), .delete, .has, .clear\n\n  console.log(cookie); // undefined\n\n  const response = NextResponse.next();\n\n  // Setting cookie\n  response.cookies.set(\"key\", \"value\");\n  response.cookies.set({\n    name: \"test\",\n    value: \"1\",\n    path: \"/\",\n  });\n\n  console.log(response.cookies.get(\"test\")); // { name: 'test', value: '1' }\n\n  return response;\n};\n```\n\n### Setting Headers\n\n```javascript\nimport { NextResponse } from \"next/server\";\n\nexport const middleware = (request) =\u003e {\n  // Getting headers object/map\n  const requestHeader = new Headers(request.headers);\n\n  // Setting header\n  requestHeader.set(\"x-test\", \"test header\");\n\n  // We can also set request headers in NextResponse.rewrite\n  const response = NextResponse.next({\n    request: {\n      // New request headers\n      headers: requestHeader,\n    },\n  });\n\n  response.headers.set(\"x-test1\", \"hello header\");\n  return response;\n};\n```\n\n## CORS\n\n```javascript\nimport { NextResponse } from \"next/server\";\n\n// Origin must included in .env file\nconst allowOrigins = [\"https://test.com\", \"http://localhost:3000\"];\n\nconst corsOrigin = {\n  \"Access-Control-Allow-Methods\": \"GET, POST, PUT, DELETE, OPTIONS\",\n  \"Access-Control-Allow-Headers\": \"Content-Type, Authorization\",\n};\n\nexport const middleware = (request) =\u003e {\n  const origin = request.headers.get(\"origin\");\n\n  const isAllowedOrigin = allowOrigins.includes(origin);\n\n  const isPreflight = request.method === \"OPTIONS\";\n\n  if (isPreflight) {\n    const preflightHeaders = {\n      ...(isAllowedOrigin \u0026\u0026 { \"Access-Control-Allow-Origin\": origin }),\n      ...corsOrigin,\n    };\n\n    return NextResponse.json({}, { headers: preflightHeaders });\n  }\n  const response = NextResponse.next();\n\n  if (isAllowedOrigin) {\n    response.headers.set(\"Access-Control-Allow-Origin\", origin);\n  }\n\n  Object.entries(corsOrigin).forEach(([key, value]) =\u003e {\n    response.headers.set(key, value);\n  });\n  return response;\n};\n\nexport const config = {\n  matcher: \"/api/test\",\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevjubayr%2Fmiddleware-nextjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevjubayr%2Fmiddleware-nextjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevjubayr%2Fmiddleware-nextjs/lists"}