Skip to content

Commit

Permalink
added noCors middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jun 4, 2024
1 parent 14b528b commit 1bacb23
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
## [2.2.1] - Unreleased
### Added
- Allow to run a server with `deno serve -A _config.ts`.
- New `noCors` middleware to prevent CORS errors during development.

### Fixed
- Port detection in `lume cms` command.
Expand Down
2 changes: 2 additions & 0 deletions cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Server from "../core/server.ts";
import { SiteWatcher } from "../core/watcher.ts";
import logger from "../middlewares/logger.ts";
import noCache from "../middlewares/no_cache.ts";
import noCors from "../middlewares/no_cors.ts";
import notFound from "../middlewares/not_found.ts";
import reload from "../middlewares/reload.ts";
import { createSite } from "./run.ts";
Expand Down Expand Up @@ -112,6 +113,7 @@ export async function build(
server.use(
reload({ watcher: new SiteWatcher(site) }),
noCache(),
noCors(),
notFound({
root,
page404,
Expand Down
11 changes: 11 additions & 0 deletions middlewares/no_cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Middleware } from "../core/server.ts";

/** Add a header to prevent CORS errors (used in development) */
export default function noCors(): Middleware {
return async (request, next) => {
const response = await next(request);
response.headers.set("access-control-allow-origin", "*");

return response;
};
}

0 comments on commit 1bacb23

Please sign in to comment.