@ludo.ninja/components 2.3.67 → 2.3.69

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.
@@ -1,8 +1,9 @@
1
1
  import { GetServerSideProps } from 'next';
2
+ export declare const hashString: (str: string) => number;
2
3
  export type TimestampProps = {
3
4
  isoTimestamp: string;
4
5
  displayTimestamp: string;
5
6
  checksum: number;
7
+ currentUrl: string;
6
8
  };
7
- export declare function withTimestampSSR<P extends Record<string, any>>(getServerSideProps: GetServerSideProps<P>): GetServerSideProps<P & TimestampProps>;
8
- export declare function withTimestampSSR(): GetServerSideProps<TimestampProps>;
9
+ export declare const withTimestampSSR: <P extends Record<string, any>>(getServerSideProps?: GetServerSideProps<P>) => GetServerSideProps<P & TimestampProps> | GetServerSideProps<TimestampProps>;
@@ -1,20 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withTimestampSSR = withTimestampSSR;
3
+ exports.withTimestampSSR = exports.hashString = void 0;
4
4
  let lastTimestamp = 0;
5
- function hashString(str) {
5
+ const hashString = (str) => {
6
6
  let hash = 0;
7
7
  for (let i = 0; i < str.length; i++) {
8
8
  hash = (hash << 5) - hash + str.charCodeAt(i);
9
9
  hash |= 0;
10
10
  }
11
11
  return Math.abs(hash);
12
- }
13
- function withTimestampSSR(getServerSideProps) {
12
+ };
13
+ exports.hashString = hashString;
14
+ const withTimestampSSR = (getServerSideProps) => {
14
15
  return async (context) => {
15
16
  const now = Date.now();
16
- const url = context.resolvedUrl || '/';
17
- const hash = hashString(url);
17
+ const req = context.req;
18
+ const protocol = req.headers['x-forwarded-proto'] || 'http';
19
+ const host = req.headers.host;
20
+ const path = context.resolvedUrl;
21
+ const fullUrl = `${protocol}://${host}${path}`;
22
+ const hash = (0, exports.hashString)(fullUrl);
18
23
  const secondsAgo = (hash % 59) + 2;
19
24
  let proposedTimestamp = now - secondsAgo * 1000;
20
25
  if (proposedTimestamp <= lastTimestamp) {
@@ -31,7 +36,7 @@ function withTimestampSSR(getServerSideProps) {
31
36
  minute: '2-digit',
32
37
  second: '2-digit',
33
38
  });
34
- const checksum = hashString(url + isoTimestamp);
39
+ const checksum = (0, exports.hashString)(fullUrl + isoTimestamp);
35
40
  if (getServerSideProps) {
36
41
  const result = await getServerSideProps(context);
37
42
  if ('props' in result) {
@@ -41,6 +46,7 @@ function withTimestampSSR(getServerSideProps) {
41
46
  isoTimestamp,
42
47
  displayTimestamp,
43
48
  checksum,
49
+ currentUrl: fullUrl,
44
50
  },
45
51
  };
46
52
  }
@@ -51,7 +57,9 @@ function withTimestampSSR(getServerSideProps) {
51
57
  isoTimestamp,
52
58
  displayTimestamp,
53
59
  checksum,
60
+ currentUrl: fullUrl,
54
61
  },
55
62
  };
56
63
  };
57
- }
64
+ };
65
+ exports.withTimestampSSR = withTimestampSSR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ludo.ninja/components",
3
- "version": "2.3.67",
3
+ "version": "2.3.69",
4
4
  "private": false,
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",