Skip to content

Commit 9338486

Browse files
committed
Rename netlify hook to deploy hook
1 parent 242d7e2 commit 9338486

File tree

9 files changed

+165
-49
lines changed

9 files changed

+165
-49
lines changed
File renamed without changes.

netlify-completion-hook/handler.js deploy-hook/handler.js

+45-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
const axios = require('axios');
2-
module.exports.webhook = async event => {
3-
if (
4-
event.queryStringParameters &&
5-
event.queryStringParameters.key !== process.env.SSR_DEPLOY_TOKEN
6-
) {
7-
return {
8-
statusCode: 401,
9-
body: JSON.stringify({
10-
error: 'Invalid Key',
11-
}),
12-
};
13-
}
2+
const Redis = require('ioredis');
143

15-
if (!event.body) {
4+
async function initiateGithubAction(body: any) {
5+
if (!body) {
166
return {
177
statusCode: 400,
188
body: JSON.stringify({
@@ -21,7 +11,7 @@ module.exports.webhook = async event => {
2111
};
2212
}
2313

24-
const parsedBody = JSON.parse(event.body);
14+
const parsedBody = JSON.parse(body);
2515
if (!['master'].includes(parsedBody.branch)) {
2616
return {
2717
statusCode: 403,
@@ -52,4 +42,45 @@ module.exports.webhook = async event => {
5242
} catch (e) {
5343
console.log(e);
5444
}
45+
}
46+
47+
async function clearSSRCache() {
48+
const redis = new Redis({
49+
host: process.env.REDIS_HOST || 'localhost',
50+
});
51+
const keys = await redis.keys('ssr:*');
52+
await Promise.all(keys.map(key => redis.del(key)));
53+
redis.disconnect();
54+
}
55+
56+
module.exports.webhook = async event => {
57+
if (!event.queryStringParameters) {
58+
return {
59+
statusCode: 401,
60+
body: JSON.stringify({
61+
error: 'Empty querystring',
62+
}),
63+
};
64+
}
65+
66+
const { key, type } = event.queryStringParameters;
67+
68+
if (key !== process.env.SSR_DEPLOY_TOKEN) {
69+
return {
70+
statusCode: 401,
71+
body: JSON.stringify({
72+
error: 'Invalid Key',
73+
}),
74+
};
75+
}
76+
77+
if (type === 'netlify-complete') {
78+
return await initiateGithubAction(event.body);
79+
} else if (type === 'ssr-deploy-complete') {
80+
await clearSSRCache();
81+
return {
82+
statusCode: 204,
83+
body: '',
84+
};
85+
}
5586
};

netlify-completion-hook/package.json deploy-hook/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"main": "index.js",
55
"license": "MIT",
66
"dependencies": {
7-
"axios": "^0.19.1"
7+
"axios": "^0.19.1",
8+
"ioredis": "^4.14.1"
89
}
910
}

netlify-completion-hook/serverless.yml deploy-hook/serverless.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@ provider:
99
environment:
1010
GITHUB_TOKEN: ${ssm:/velog-v2/github-token}
1111
SSR_DEPLOY_TOKEN: ${ssm:/velog-v2/ssr-deploy-token}
12+
REDIS_HOST: ${ssm:/velog-v2/redis-host}
13+
14+
vpc:
15+
securityGroupIds:
16+
- sg-007a5a395dbdcef1f
17+
- sg-f588c99d
18+
- sg-0fd14591289bb3212
19+
subnetIds:
20+
- subnet-02c6112f71f5148c7
21+
- subnet-0ebc43e6ab298c646
1222

1323
functions:
1424
webhook:
1525
handler: handler.webhook
1626
events:
1727
- http:
1828
path: /
19-
method: POST
29+
method: POST
30+

deploy-hook/yarn.lock

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
axios@^0.19.1:
6+
version "0.19.1"
7+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.1.tgz#8a6a04eed23dfe72747e1dd43c604b8f1677b5aa"
8+
integrity sha512-Yl+7nfreYKaLRvAvjNPkvfjnQHJM1yLBY3zhqAwcJSwR/6ETkanUgylgtIvkvz0xJ+p/vZuNw8X7Hnb7Whsbpw==
9+
dependencies:
10+
follow-redirects "1.5.10"
11+
12+
cluster-key-slot@^1.1.0:
13+
version "1.1.0"
14+
resolved "https://registry.yarnpkg.com/cluster-key-slot/-/cluster-key-slot-1.1.0.tgz#30474b2a981fb12172695833052bc0d01336d10d"
15+
integrity sha512-2Nii8p3RwAPiFwsnZvukotvow2rIHM+yQ6ZcBXGHdniadkYGZYiGmkHJIbZPIV9nfv7m/U1IPMVVcAhoWFeklw==
16+
17+
debug@=3.1.0:
18+
version "3.1.0"
19+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
20+
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
21+
dependencies:
22+
ms "2.0.0"
23+
24+
debug@^4.1.1:
25+
version "4.1.1"
26+
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
27+
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
28+
dependencies:
29+
ms "^2.1.1"
30+
31+
denque@^1.1.0:
32+
version "1.4.1"
33+
resolved "https://registry.yarnpkg.com/denque/-/denque-1.4.1.tgz#6744ff7641c148c3f8a69c307e51235c1f4a37cf"
34+
integrity sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==
35+
36+
37+
version "1.5.10"
38+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
39+
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
40+
dependencies:
41+
debug "=3.1.0"
42+
43+
ioredis@^4.14.1:
44+
version "4.14.1"
45+
resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-4.14.1.tgz#b73ded95fcf220f106d33125a92ef6213aa31318"
46+
integrity sha512-94W+X//GHM+1GJvDk6JPc+8qlM7Dul+9K+lg3/aHixPN7ZGkW6qlvX0DG6At9hWtH2v3B32myfZqWoANUJYGJA==
47+
dependencies:
48+
cluster-key-slot "^1.1.0"
49+
debug "^4.1.1"
50+
denque "^1.1.0"
51+
lodash.defaults "^4.2.0"
52+
lodash.flatten "^4.4.0"
53+
redis-commands "1.5.0"
54+
redis-errors "^1.2.0"
55+
redis-parser "^3.0.0"
56+
standard-as-callback "^2.0.1"
57+
58+
lodash.defaults@^4.2.0:
59+
version "4.2.0"
60+
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
61+
integrity sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=
62+
63+
lodash.flatten@^4.4.0:
64+
version "4.4.0"
65+
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
66+
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=
67+
68+
69+
version "2.0.0"
70+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
71+
integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=
72+
73+
ms@^2.1.1:
74+
version "2.1.2"
75+
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
76+
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
77+
78+
79+
version "1.5.0"
80+
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.5.0.tgz#80d2e20698fe688f227127ff9e5164a7dd17e785"
81+
integrity sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==
82+
83+
redis-errors@^1.0.0, redis-errors@^1.2.0:
84+
version "1.2.0"
85+
resolved "https://registry.yarnpkg.com/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad"
86+
integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=
87+
88+
redis-parser@^3.0.0:
89+
version "3.0.0"
90+
resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4"
91+
integrity sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=
92+
dependencies:
93+
redis-errors "^1.0.0"
94+
95+
standard-as-callback@^2.0.1:
96+
version "2.0.1"
97+
resolved "https://registry.yarnpkg.com/standard-as-callback/-/standard-as-callback-2.0.1.tgz#ed8bb25648e15831759b6023bdb87e6b60b38126"
98+
integrity sha512-NQOxSeB8gOI5WjSaxjBgog2QFw55FV8TkS6Y07BiB3VJ8xNTvUYm0wl0s8ObgQ5NhdpnNfigMIKjgPESzgr4tg==

netlify-completion-hook/yarn.lock

-29
This file was deleted.

src/server/CacheManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Redis from 'ioredis';
22
import checkCacheRule from './checkCacheRule';
33

44
const redis = new Redis({
5-
host: process.env.REDIS_HOST || 'locahost',
5+
host: process.env.REDIS_HOST || 'localhost',
66
});
77

88
function createCacheKey(url: string) {

src/server/serverRender.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ const serverRender = async ({ url, loggedIn, cookie }: SSROption) => {
4141
if (!loggedIn) {
4242
const cachedPage = await cacheManager.get(url);
4343
if (cachedPage) {
44-
// Temp disable cache
45-
// return cachedPage;
44+
return {
45+
html: cachedPage,
46+
statusCode: 200,
47+
};
4648
}
4749
}
4850

@@ -69,6 +71,8 @@ const serverRender = async ({ url, loggedIn, cookie }: SSROption) => {
6971

7072
const helmetContext = {} as FilledContext;
7173

74+
console.log('URL: ', url);
75+
7276
const Root = (
7377
<ChunkExtractorManager extractor={extractor}>
7478
<HelmetProvider context={helmetContext}>

src/serverless.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const handler = async (event: APIGatewayEvent) => {
66
const query = event.queryStringParameters
77
? qs.stringify(event.queryStringParameters)
88
: '';
9-
const url = event.path.concat(query);
9+
const url = query ? event.path.concat('?', query) : event.path;
1010
const cookie = event.headers.Cookie || '';
1111
const loggedIn =
1212
cookie.includes('refresh_token') || cookie.includes('access_token');

0 commit comments

Comments
 (0)