@ludo.ninja/components 2.3.66 → 2.3.68

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.
@@ -0,0 +1,9 @@
1
+ import { GetServerSideProps } from 'next';
2
+ export declare const hashString: (str: string) => number;
3
+ export type TimestampProps = {
4
+ isoTimestamp: string;
5
+ displayTimestamp: string;
6
+ checksum: number;
7
+ currentUrl: string;
8
+ };
9
+ export declare const withTimestampSSR: <P extends Record<string, any>>(getServerSideProps?: GetServerSideProps<P>) => GetServerSideProps<P & TimestampProps> | GetServerSideProps<TimestampProps>;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTimestampSSR = exports.hashString = void 0;
4
+ let lastTimestamp = 0;
5
+ const hashString = (str) => {
6
+ let hash = 0;
7
+ for (let i = 0; i < str.length; i++) {
8
+ hash = (hash << 5) - hash + str.charCodeAt(i);
9
+ hash |= 0;
10
+ }
11
+ return Math.abs(hash);
12
+ };
13
+ exports.hashString = hashString;
14
+ const withTimestampSSR = (getServerSideProps) => {
15
+ return async (context) => {
16
+ const now = Date.now();
17
+ const currentUrl = context.resolvedUrl || '/';
18
+ const hash = (0, exports.hashString)(currentUrl);
19
+ const secondsAgo = (hash % 59) + 2;
20
+ let proposedTimestamp = now - secondsAgo * 1000;
21
+ if (proposedTimestamp <= lastTimestamp) {
22
+ proposedTimestamp = lastTimestamp + 1000;
23
+ }
24
+ lastTimestamp = proposedTimestamp;
25
+ const updatedDate = new Date(proposedTimestamp);
26
+ const isoTimestamp = updatedDate.toISOString();
27
+ const displayTimestamp = updatedDate.toLocaleString('en-GB', {
28
+ day: '2-digit',
29
+ month: 'long',
30
+ year: 'numeric',
31
+ hour: '2-digit',
32
+ minute: '2-digit',
33
+ second: '2-digit',
34
+ });
35
+ const checksum = (0, exports.hashString)(currentUrl + isoTimestamp);
36
+ if (getServerSideProps) {
37
+ const result = await getServerSideProps(context);
38
+ if ('props' in result) {
39
+ return {
40
+ props: {
41
+ ...result.props,
42
+ isoTimestamp,
43
+ displayTimestamp,
44
+ checksum,
45
+ currentUrl,
46
+ },
47
+ };
48
+ }
49
+ return result;
50
+ }
51
+ return {
52
+ props: {
53
+ isoTimestamp,
54
+ displayTimestamp,
55
+ checksum,
56
+ currentUrl,
57
+ },
58
+ };
59
+ };
60
+ };
61
+ exports.withTimestampSSR = withTimestampSSR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ludo.ninja/components",
3
- "version": "2.3.66",
3
+ "version": "2.3.68",
4
4
  "private": false,
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",