Skip to content

Commit

Permalink
refactor!: change config to utility, to get config
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Sep 7, 2021
1 parent 0905dfb commit 2531dc8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ In your project's configuration, you can add something like:
// WARN: if this is a png or jpg, it will be served as base64 string if small
import favicon from '@package/components/layout/favicon.ico';

config.settings.staticFiles = {
'favicon.ico': {
url: favicon,
contentType: 'image/ico',
},
};
import { serveStaticResources } from 'volto-middleware-static';

if (__SERVER__) {
config.settings.expressMiddleware = [
...config.settings.expressMiddleware,
serveStaticResources({
'favicon.ico': {
url: favicon,
contentType: 'image/ico',
},
}),
];
}
```

And then you'll have Volto serving your favicon and you can customize it from your code.
Expand Down
21 changes: 13 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ const getResource = (url) =>
}
});

export default function applyConfig(config) {
const { staticFiles } = config.settings;
/**
* Create a middleware to serve static files
* @param {{[string]: {url: string, contentType: string}}} staticFiles - contains the static files to be served
* @returns {express.IRouter} staticResourcesMiddleware
*/
export function serveStaticResources(staticFiles = {}) {
// EXAMPLE:
// config.settings.staticFiles = {
// {
// 'favicon.ico': {
// url: favicon, // webpack loaded resource
// contentType: 'image/ico',
// },
// };

if (__SERVER__ && staticFiles) {
if (__SERVER__) {
const express = require('express');

const staticResourcesMiddleware = express.Router();
Expand All @@ -44,11 +48,12 @@ export default function applyConfig(config) {
next();
});

config.settings.expressMiddleware = [
...config.settings.expressMiddleware,
staticResourcesMiddleware,
];
return staticResourcesMiddleware;
}

return {};
}

export default function applyConfig(config) {
return config;
}

0 comments on commit 2531dc8

Please sign in to comment.