-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
65 lines (54 loc) · 1.63 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const got = require('got');
const path = require('path');
const dotenv = require('dotenv');
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');
// Dotenv
dotenv.config();
// Constants
const { GATSBY_API_URL } = process.env;
const movieTemplate = path.resolve(`src/modules/movie/pages/Movie.js`);
// Dynamic pages
exports.createPages = async function({ actions, graphql }) {
const { createPage, createRedirect } = actions;
try {
const results = await graphql(`
query {
estrenos {
movies {
id
slug
}
}
}
`);
if (results.errors) {
console.error('Result errors in createPage', results.errors);
}
const { movies } = results.data.estrenos;
const cinemas = await got(`${GATSBY_API_URL}/cinemas`).json();
for await (const edge of movies) {
const { id, slug } = edge;
const [movie, shows] = await Promise.all([
got(`${GATSBY_API_URL}/movies/${id}`).json(),
got(`${GATSBY_API_URL}/shows/${id}`).json(),
]);
createPage({
component: movieTemplate,
path: `/peliculas/${slug}`,
context: { id, slug, cinemas, movie, shows },
});
}
createRedirect({ fromPath: '/peliculas', toPath: '/', isPermanent: true });
} catch (error) {
console.error('Error in static page creation', error);
}
};
// Webpack
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
actions.setWebpackConfig({
module: {
rules: stage === 'build-html' ? [{ test: /mapbox-gl/, use: loaders.null() }] : [],
},
plugins: [new AntdDayjsWebpackPlugin()],
});
};