5htp-core 0.5.0-9 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.5.0-9",
4
+ "version": "0.5.1",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -80,6 +80,7 @@ export type TRouteOptions = {
80
80
  accept?: string,
81
81
  raw?: boolean, // true to return raw data
82
82
  auth?: TUserRole | boolean,
83
+ canonicalParams?: string[],
83
84
 
84
85
  // Rendering
85
86
  static?: boolean,
@@ -490,7 +490,6 @@ declare type Routes = {
490
490
 
491
491
  // Run on resolution hooks. Ex: authentication check
492
492
  await this.runHook('resolved', route);
493
-
494
493
  const timeEndResolving = Date.now();
495
494
 
496
495
  // Create response
@@ -82,6 +82,7 @@ export default class ServerResponse<
82
82
  public headers: {[cle: string]: string} = {}
83
83
  public cookie: express.Response["cookie"];
84
84
  public clearCookie: express.Response["clearCookie"];
85
+ public canonicalUrl: URL;
85
86
 
86
87
  // If data was provided by at lead one controller
87
88
  public wasProvided = false;
@@ -95,12 +96,18 @@ export default class ServerResponse<
95
96
 
96
97
  this.router = request.router;
97
98
  this.app = this.router.app;
99
+
100
+ this.canonicalUrl = new URL(request.url);
101
+ this.canonicalUrl.search = '';
98
102
  }
99
103
 
100
104
  public async runController( route: TAnyRoute, additionnalData: {} = {} ) {
101
105
 
102
106
  this.route = route;
103
107
 
108
+ // Update canonical url
109
+ this.updateCanonicalUrl(route);
110
+
104
111
  // Create response context for controllers
105
112
  const context = await this.createContext(route);
106
113
 
@@ -140,6 +147,18 @@ export default class ServerResponse<
140
147
  this.router.cache[ chunkId ] = this.data;
141
148
  }
142
149
 
150
+ private updateCanonicalUrl( route: TAnyRoute ) {
151
+
152
+ if (!route.options.canonicalParams)
153
+ return;
154
+
155
+ for (const key of route.options.canonicalParams) {
156
+ const paramValue = this.request.data[ key ];
157
+ if (paramValue !== undefined)
158
+ this.canonicalUrl.searchParams.set(key, paramValue);
159
+ }
160
+ }
161
+
143
162
  /*----------------------------------
144
163
  - INTERNAL
145
164
  ----------------------------------*/
@@ -57,9 +57,6 @@ export default class DocumentRenderer<TRouter extends Router> {
57
57
 
58
58
  public async page( html: string, page: Page, response: ServerResponse<TRouter> ) {
59
59
 
60
- // TODO: can be customized via page / route config
61
- const canonicalUrl = response.request.url;
62
-
63
60
  let attrsBody = {
64
61
  className: [...page.bodyClass].join(' '),
65
62
  };
@@ -94,7 +91,7 @@ export default class DocumentRenderer<TRouter extends Router> {
94
91
  {/* Page */}
95
92
  <title>{page.title}</title>
96
93
  <meta content={page.description} name="description" />
97
- <link rel="canonical" href={canonicalUrl} />
94
+ <link rel="canonical" href={response.canonicalUrl} />
98
95
 
99
96
  {/* SEO, social medias, OG tags, ... */}
100
97
  {page.head.map(({ $, ...attrs }) => (