-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.conf.js
93 lines (79 loc) · 3.51 KB
/
proxy.conf.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
When the app runs in disconnected mode, and Sitecore is not present, we need to give
the app copies of the Sitecore APIs it depends on (layout service, dictionary service, content service)
to talk to so that the app can run using the locally defined disconnected data.
This is accomplished by spinning up a small Express server that mocks the APIs, and then
telling angular-cli to proxy requests to the API paths to this express instance.
Customizing these fake services may be required if your app has advanced data requirements,
or has other backend services that it requires. See the angular advanced sample for examples
of doing this, as well as how to proxy the dictionary and content services.
*/
const express = require('express');
const config = require('./package.json').config;
const {
createDisconnectedLayoutService,
createDisconnectedContentService,
createDisconnectedDictionaryService,
ManifestManager,
} = require('@sitecore-jss/sitecore-jss-dev-tools');
const port = 3042;
const app = express();
const appRoot = __dirname;
// the manifest manager maintains the state of the disconnected manifest data during the course of the dev run
// it provides file watching services, and language switching capabilities
const manifestManager = new ManifestManager({
appName: config.appName,
rootPath: appRoot,
watchOnlySourceFiles: './data/**',
});
manifestManager
.getManifest(config.language)
.then((manifest) => {
// creates a fake version of the Sitecore Layout Service that is powered by your disconnected manifest file
const layoutService = createDisconnectedLayoutService({
manifest,
manifestLanguageChangeCallback: manifestManager.getManifest,
});
// creates a fake version of the Sitecore Dictionary Service that is powered by your disconnected manifest file
const dictionaryService = createDisconnectedDictionaryService({
manifest,
manifestLanguageChangeCallback: manifestManager.getManifest,
});
// creates a fake version of the Sitecore Content Service that is powered by your disconnected manifest file
const contentService = createDisconnectedContentService({
manifest,
manifestLanguageChangeCallback: manifestManager.getManifest,
});
// set up live reloading of the manifest when any manifest source file is changed
manifestManager.setManifestUpdatedCallback((newManifest) => {
layoutService.updateManifest(newManifest);
dictionaryService.updateManifest(newManifest);
contentService.updateManifest(newManifest);
console.log('Manifest data updated. Refresh the browser to see latest content.');
});
// attach our disconnected service mocking middleware to webpack dev server
app.use('/data', express.static('data'));
app.use('/sitecore/api/layout/render', layoutService.middleware);
app.use('/sitecore/api/jss/dictionary/:appName/:language', dictionaryService.middleware);
app.use('/sitecore/api/jss/contentsvc', contentService.middleware);
app.listen(port, () => {
console.log(`Sitecore data mock listening on port ${port}!`);
});
})
.catch((error) => {
console.error(error);
process.exit(1);
});
const PROXY_CONFIG = [
{
context: [
'/data',
'/sitecore/api/layout/render',
'/sitecore/api/jss/dictionary',
'/sitecore/api/jss/contentsvc',
],
target: `http://localhost:${port}`,
secure: false,
},
];
module.exports = PROXY_CONFIG;