Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit 75113f3

Browse files
committed
Now supports a prefix path with static images
1 parent 2f66210 commit 75113f3

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ This setting is used to make the plugin download binary assets into the public d
9595
**`staticAssetRootDir`** [optional]
9696
This setting is ignored unless staticAssetDownload is set to true. This allows a developer to define a directory in the website that will contain all the downloaded assets. This can help to segregate the items coming from the server from other static data. The default value is "asset", but it can be set to any string that is a valid directory name.
9797

98+
**`staticUrlPrefix`** [optional]
99+
This setting is ignored unless staticAssetDownload is set to true. This needs to be set if the application has a pathPrefix defined in its gatsby-config.js file. In static mode all downloaded files are given a URL relative to the build root (/public) of the application by default. If there is a pathPrefix specified though, then the URLs need to be prefixed with that value as well.
100+
For example: If there is a file "logo.png" it might be given a default URL of /server/logo.png . If however, the app uses a pathPrefix of /myApplication, this URL should be changed to /myApplication/server/logo.png.
101+
Setting staticUrlPrefix equal to the pathPrefix will cause the plugin to make this adjustment in the Gatsby cache.
102+
103+
98104
**`How to use static download:`**
99105
Assume there is a digital asset on the server called Logo.jpg that contains an image to be used in a site.
100106
If staticAssetDownload is false then the binary image will be stored in the cache as well as any selected renditions. (See 'renditions' flag above) These binary files can then be queried and traversed via GraphQL

gatsby-node.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const fetch = require('./src/fetch');
3030
* renditions: 'all',
3131
* staticAssetDownload: false
3232
* staticAssetRootDir: 'asset'
33+
* staticUrlPrefix: ''
3334
* },
3435
* },
3536
* ]
@@ -66,7 +67,10 @@ const fetch = require('./src/fetch');
6667
* <b>staticAssetRootDir</b> this setting is only used if staticAssetDownload is true. It is used
6768
* to set a a prefix that will be prepended to all of the urls for downloaded files. For example:
6869
* If it is set to "content" then the url of a downloaded image called Logo.jpg
69-
* will be /content/Logo.jpg.
70+
* will be /content/Logo.jpg.<br>
71+
* <b>staticUrlPrefix</b> (optional) should be set equal to the pathPrefix defined in the
72+
* gatsby-config.js file. If pathPrefix is not used or staticAssetDownload is false then this
73+
* parameter doesn't need to be set.
7074
*
7175
*
7276
*/
@@ -86,7 +90,7 @@ exports.sourceNodes = async (
8690

8791
const {
8892
contentServer, channelToken, proxyUrl = '', items: { limit } = 100, items: { query } = '', auth = '', preview = false, renditions = 'custom',
89-
staticAssetDownload = false, staticAssetRootDir = 'assets', debug = false,
93+
staticAssetDownload = false, staticAssetRootDir = 'assets', staticUrlPrefix = '', debug = false,
9094
} = configOptions;
9195

9296
console.log('Using OCE plugin with the options: ', {
@@ -100,6 +104,7 @@ exports.sourceNodes = async (
100104
renditions,
101105
staticAssetDownload,
102106
staticAssetRootDir,
107+
staticUrlPrefix,
103108
debug,
104109
});
105110
try {
@@ -132,6 +137,7 @@ exports.sourceNodes = async (
132137
await process.downloadMediaFilesToStaticDir({
133138
entities,
134139
staticAssetRootDir,
140+
staticUrlPrefix,
135141
renditions,
136142
auth,
137143
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@oracle/gatsby-source-oce",
33
"description": "Gatsby source plugin for using Oracle Content Management as a data source",
44
"author": "Oracle America, Inc.",
5-
"version": "1.1.0",
5+
"version": "1.1.1",
66
"homepage": "https://github.com/oracle/gatsby-source-oce",
77
"repository": {
88
"type": "git",

src/process.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ exports.moveFieldsUp = (entities) => entities
8888

8989
// const pretty = (o) => JSON.stringify(o, null, 2);
9090

91-
const getMediaList = (element, renditions, staticRootDir = null) => {
91+
const getMediaList = (element, renditions, staticRootDir = null, staticUrlPrefix = '') => {
9292
const result = [];
9393
const baseKey = `oce-media-${element.id}`;
9494
// Get the asset name less the extension
@@ -106,7 +106,7 @@ const getMediaList = (element, renditions, staticRootDir = null) => {
106106
});
107107
// Only do this if we are downloading the asset to be referenced as a public url in the site
108108
if (staticRootDir) {
109-
element.staticURL = `/${staticRootDir}/${element.name}`;
109+
element.staticURL = `${staticUrlPrefix}/${staticRootDir}/${element.name}`;
110110
}
111111

112112
// Now loop through renditions and grab the first version of each. The name of the rendition
@@ -126,7 +126,7 @@ const getMediaList = (element, renditions, staticRootDir = null) => {
126126
});
127127
// Only do this if we are downloading the asset to be referenced as a public url in the site
128128
if (staticRootDir) {
129-
e.staticURL = `/${staticRootDir}/${e.name}/${element.name}`;
129+
e.staticURL = `${staticUrlPrefix}/${staticRootDir}/${e.name}/${element.name}`;
130130
}
131131
}
132132
});
@@ -186,6 +186,7 @@ async function downloadToPublic(
186186
exports.downloadMediaFilesToStaticDir = async ({
187187
entities,
188188
staticAssetRootDir,
189+
staticUrlPrefix,
189190
renditions,
190191
auth,
191192
}) => {
@@ -195,7 +196,7 @@ exports.downloadMediaFilesToStaticDir = async ({
195196
for (let idx = 0; idx < entities.length; idx++) {
196197
const e = entities[idx];
197198
if (isDigitalAsset(e)) {
198-
const dataItemList = getMediaList(e, renditions, staticAssetRootDir);
199+
const dataItemList = getMediaList(e, renditions, staticAssetRootDir, staticUrlPrefix);
199200
for (let mediaIndex = 0; mediaIndex < dataItemList.length; mediaIndex++) {
200201
const mediaObject = dataItemList[mediaIndex];
201202
const storagePath = `./public/${staticAssetRootDir}/${mediaObject.staticSubDir}`;

0 commit comments

Comments
 (0)