Skip to content

Commit

Permalink
feat(underscore-redirects): add support for force (#11271)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Lu <[email protected]>
  • Loading branch information
florian-lefebvre and bluwy authored Jun 20, 2024
1 parent 5848d97 commit 7f956f0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .changeset/funny-cats-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@astrojs/underscore-redirects': patch
---

Adds support for forced redirects

Redirects can be forced by setting `force` to `true`:

```ts
redirects.add({
// ...
force: true
})
```

It will append a `!` after the status.
3 changes: 2 additions & 1 deletion packages/underscore-redirects/src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function print(
' '.repeat(inputSpaces) +
definition.target +
' '.repeat(Math.abs(targetSpaces)) +
definition.status;
definition.status +
(definition.force ? '!' : '');
}

return _redirects;
Expand Down
1 change: 1 addition & 0 deletions packages/underscore-redirects/src/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type RedirectDefinition = {
// a priority once inserted.
weight: number;
status: number;
force?: number;
};

export class Redirects {
Expand Down
17 changes: 17 additions & 0 deletions packages/underscore-redirects/test/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ describe('Printing', () => {
const expectedParts = ['/pets/:cat', '/pets/:cat/index.html', '200'];
assert.deepEqual(parts, expectedParts);
});

it('Properly handles force redirects', () => {
const _redirects = new Redirects();
_redirects.add({
dynamic: false,
input: '/a',
target: '/b',
status: 200,
weight: 1,
force: true
});
let out = _redirects.print();
let parts = out.split(/\s+/);

const expectedParts = ['/a', '/b', '200!'];
assert.deepEqual(parts, expectedParts);
})
});

0 comments on commit 7f956f0

Please sign in to comment.