Skip to content

Commit 9d79ec6

Browse files
authored
docs(docs-infra): introduce a custom UrlSerializer
The custom serializer should handle Adev-specific behavior like decoding encoded forward slash similarly to the app host.
1 parent fcd0bb0 commit 9d79ec6

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

adev/src/app/app.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
inject,
1414
provideEnvironmentInitializer,
1515
} from '@angular/core';
16+
import {UrlSerializer} from '@angular/router';
1617
import {
1718
DOCS_CONTENT_LOADER,
1819
ENVIRONMENT,
@@ -31,6 +32,7 @@ import {CustomErrorHandler} from './core/services/errors-handling/error-handler'
3132
import {ExampleContentLoader} from './core/services/example-content-loader.service';
3233
import {routerProviders} from './routing/router_providers';
3334
import {TYPESCRIPT_VFS_WORKER_PROVIDER} from './editor/code-editor/workers/factory-provider';
35+
import {AdevUrlSerializer} from './core/services/routing/adev-url-serializer';
3436

3537
export const appConfig: ApplicationConfig = {
3638
providers: [
@@ -49,5 +51,9 @@ export const appConfig: ApplicationConfig = {
4951
deps: [DOCUMENT],
5052
},
5153
TYPESCRIPT_VFS_WORKER_PROVIDER,
54+
{
55+
provide: UrlSerializer,
56+
useClass: AdevUrlSerializer,
57+
},
5258
],
5359
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {DefaultUrlSerializer, UrlTree} from '@angular/router';
10+
11+
/**
12+
* Custom URL serializer extending the default behavior
13+
* with Adev-specific behavior.
14+
*/
15+
export class AdevUrlSerializer extends DefaultUrlSerializer {
16+
override parse(url: string): UrlTree {
17+
// Since the app host/server is decoding encoded forward slashes,
18+
// we perform this on the client as well in order to maintain
19+
// a consistent behavior between the two environments and
20+
// avoid opening a different page on client hydration (presumably, 404).
21+
url = url.replaceAll(/%2(F|f)/g, '/');
22+
23+
return super.parse(url);
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {TestBed} from '@angular/core/testing';
10+
import {UrlSerializer} from '@angular/router';
11+
import {AdevUrlSerializer} from './adev-url-serializer';
12+
13+
describe('AdevUrlSerializer', () => {
14+
let serializer: UrlSerializer;
15+
16+
beforeEach(() => {
17+
TestBed.configureTestingModule({
18+
providers: [
19+
{
20+
provide: UrlSerializer,
21+
useClass: AdevUrlSerializer,
22+
},
23+
],
24+
});
25+
26+
serializer = TestBed.inject(UrlSerializer);
27+
});
28+
29+
it('should decode encoded forward slash (%2F)', () => {
30+
// Uppercase hex
31+
expect(serializer.parse('page%2Fabout').toString()).toBe('/page/about');
32+
33+
// Lowercase hex
34+
expect(serializer.parse('page%2fabout').toString()).toBe('/page/about');
35+
});
36+
});

0 commit comments

Comments
 (0)