-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from igor-starostenko/develop
v2.3.2
- Loading branch information
Showing
16 changed files
with
246 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ frontend: | |
cache: | ||
paths: | ||
- node_modules/**/* | ||
- public/images/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
const imageOptimization = (process.env.IMAGE_OPTIMIZATION || '1') === '1'; | ||
|
||
const next_config = { | ||
images: { | ||
domains: ['images.ctfassets.net'], | ||
...{ ...(imageOptimization ? {} : { loader: 'custom' }) }, | ||
loader: 'custom', | ||
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384], | ||
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840], | ||
}, | ||
env: { | ||
imageOptimization, | ||
nextImageExportOptimizer_imageFolderPath: 'public/images', | ||
nextImageExportOptimizer_exportFolderPath: 'out', | ||
nextImageExportOptimizer_quality: 50, | ||
nextImageExportOptimizer_storePicturesInWEBP: true, | ||
nextImageExportOptimizer_generateAndUseBlurImages: true, | ||
}, | ||
productionBrowserSourceMaps: true, | ||
}; | ||
|
||
module.exports = { ...next_config }; | ||
module.exports = next_config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const fs = require('fs'); | ||
const fetch = require('node-fetch'); | ||
const contentful = require('contentful'); | ||
require('dotenv').config(); | ||
|
||
const imageDirectory = process.env.IMAGE_DIRECTORY || './public/images/'; | ||
const replaceImages = parseInt(process.env.REPLACE_IMAGES || '0'); | ||
const limit = parseInt(process.env.CONTENTFUL_LIMIT || '100'); | ||
|
||
const client = contentful.createClient({ | ||
space: process.env.CONTENTFUL_SPACE_ID, | ||
accessToken: process.env.CONTENTFUL_DELIVERY_TOKEN, | ||
}); | ||
|
||
const saveFile = async (i, url, name) => { | ||
const path = imageDirectory + name; | ||
if (!replaceImages && fs.existsSync(path)) { | ||
return console.log(`${i + 1}. ${path} already exists`); | ||
} | ||
|
||
const response = await fetch(url); | ||
const buffer = await response.buffer(); | ||
fs.writeFile(path, buffer, () => | ||
console.log(`${i + 1}. Saved ${url} into ${path}`) | ||
); | ||
}; | ||
|
||
const getAllAssets = async (options) => { | ||
let records = { items: [] }; | ||
// Ensure we load all records, regardless of the API limit | ||
while (records.total != 0 && records.items.length < (records.total || 1)) { | ||
let response = await client.getAssets({ | ||
...options, // skip can't be overriden | ||
skip: records.items.length, | ||
}); | ||
records = { ...response, items: [...records.items, ...response.items] }; | ||
} | ||
return records; | ||
}; | ||
|
||
const saveAllAssets = async (items) => { | ||
for (let i = 0; i < items.length; i += 1) { | ||
const src = `https:${items[i].fields.file.url}`; | ||
const fileName = `${items[i].sys.id}_${items[i].fields.file.fileName}`; | ||
await saveFile(i, src, fileName); | ||
} | ||
}; | ||
|
||
(async () => { | ||
const assets = await getAllAssets({ limit }); | ||
console.log(`Total assets: ${assets.total}`); | ||
await saveAllAssets(assets.items); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { SImage } from './image.css'; | ||
|
||
const noOptimization = ({ src }) => src; | ||
|
||
/* For contentful query params see | ||
https://www.contentful.com/developers/docs/references/images-api/#/reference | ||
*/ | ||
const BaseImage = ({ alt, src, query = '?fm=webp', ...rest }) => ( | ||
<SImage | ||
alt={alt} | ||
src={src + query} | ||
{...(process.env.imageOptimization | ||
? {} // image optimization is enabled by default with SSR | ||
: { | ||
loader: noOptimization, | ||
unoptimized: true, | ||
})} | ||
{...rest} | ||
/> | ||
); | ||
const BaseImage = ({ alt, src, backupSrc, ...rest }) => { | ||
const [isError, setIsError] = useState(false); | ||
|
||
if (isError) { | ||
/* eslint-disable @next/next/no-img-element */ | ||
return <img src={backupSrc} alt={alt} {...rest} />; | ||
} | ||
|
||
return ( | ||
<SImage src={src} alt={alt} onError={() => setIsError(true)} {...rest} /> | ||
); | ||
}; | ||
|
||
BaseImage.propTypes = { | ||
src: PropTypes.string.isRequired, | ||
alt: PropTypes.string.isRequired, | ||
query: PropTypes.string, | ||
backupSrc: PropTypes.string, | ||
}; | ||
|
||
export default BaseImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.