![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
electrode-react-webapp
Advanced tools
This module helps render and serve your Electrode React application's index.html
. It will handle doing server side rendering and embedding all the necessary JS and CSS bundles for your application.
All the defaults are configured out of the box, but your index page is extensible. You can specify your own index template file with the htmlFile
or selectTemplate
options.
See design for details on how the template can be extended.
$ npm install electrode-react-webapp --save
You can use this plugin by registering it with your Hapi server.
const reactWebapp = server.register({
register: require("electrode-react-webapp"),
options: {
pageTitle: "My Awesome React WebApp",
paths: {
"/{args*}": {
content: "<h1>Hello React!</h1>"
}
}
}
});
To use this with electrode-server, add to the config you pass to electrode-server
:
const config = {
plugins: {
"electrode-react-webapp": {
options: {
pageTitle: "My Awesome React WebApp",
paths: {
"/{args*}": {
content: "<h1>Hello React!</h1>"
}
},
unbundledJS: {
enterHead: [{ src: "http://cdn.com/js/lib.js" }]
}
}
}
}
};
require("electrode-server")(config);
This plugin has some default options but you can override them by setting your own value.
The current defaults are:
{
pageTitle: "Untitled Electrode Web Application",
webpackDev: process.env.WEBPACK_DEV === "true",
renderJS: true,
serverSideRendering: true,
htmlFile: "node_modules/electrode-react-webapp/lib/index.html",
devServer: {
host: "127.0.0.1",
port: "2992"
},
paths: {},
stats: "dist/server/stats.json"
}
name | type | default | description |
---|---|---|---|
pageTitle | String | The value to be shown in the browser's title bar | |
htmlFile | String | *1 | Absolute or relative path to the HTML template. |
selectTemplate | Function | Callback to selecte HTML template base on request | |
serverSideRendering | Boolean | false | Toggle server side rendering. |
webpackDev | Boolean | false | Running with webpack-dev-server |
paths | Object | Specify route paths and content | |
unbundledJS | Object | Load external JavaScript into page | |
devServer | Object | webpack Dev Server Options | |
prodBundleBase | String | "/js/" | Base path to the JavaScript, CSS and manifest bundles |
cspNonceValue | varies | Used to retrieve a CSP nonce value. |
*1
: Default forhtmlFile
is to use this module's built-inindex.html
Example:
{
paths: {
"/test": {
// route specific options
}
}
}
Route speicific options can be:
name | type | description |
---|---|---|
htmlFile | String | Absolute or relative path to the HTML template file. |
templateFile | String | |
insertTokenIds | Boolean | |
pageTitle | String | |
selectTemplate | Function | Callback to selecte HTML template for the route base on request |
responseForBadStatus | Function | |
responseForError | Function |
| content
| varies
| Content generator for server-side rendering |
| overrideOptions
| Object
| Specify any config for the given path to override top level options |
unbundledJS
DetailsExample:
{
unbundledJS: {
enterHead: [],
preBundle: []
}
}
enterHead
- Array of script entries to be inserted in <head>
before anything elsepreBundle
- Array of script entries to be inserted in <body>
before the application's bundled JavaScriptThe script entries can be:
{ src: "path to file" }
to insert a <script>
that loads a file with src
{ src: "...", async: true }
or { src: "...", defer: true }
<script>
tagshost
(String)
The host that webpack-dev-server runs onport
(String)
The port that webpack-dev-server runs onThe entry can be a Function
, Object
, or undefined
:
Function
- called with (request, type)
, where type
can be 'script'
or 'style'
Object
- it may have properties script
, style
or both, and the value for each should be the path from the request object to the nonce value
request.plugins.myCspGenerator.nonce
, then you set cspNonceValue
to { script: 'plugins.myCspGenerator.nonce' }
.
script
or style
elements that directly contain scripts or stylehtmlFile
view detailsThe top level options can specify an optional value htmlFile
.
Also each path can specify the same option to override the top level one.
This option specifies the view template for rendering your path's HTML output.
If it's undefined
, then the built-in index.html
view template is used.
If it's a string, then it must point to a valid view template file.
process.cwd()
is prepended.You can see this module's build-in index.html for details on how to create your own view template.
See design doc for details on extending the template.
The content you specify for your path is the entry to your React application when doing Server Side Rendering. Ultimately, a string
should be acquired from the content and will replace the SSR_CONTENT
marker in the view.
It can be a string, a function, or an object.
string
If it's a string, it's treated as a straight plain HTML to be inserted as the SSR_CONTENT
.
function
If it's a function, the function will be called with the web server's request
object, and it should return a promise that resolves an object:
function myContent(req) {
return Promise.resolve({
status: 200,
html: "<h1>Hello React!</h1>",
prefetch: ""
});
}
In an Electrode app, the module electrode-redux-router-engine
and its render
method is used to invoke the React component that's been specified for the route and the renderToString
output is returned as the html
.
object
If it's an object, it can specify a module
field which is the name of a module that will be require
ed. The module should export either a string or a function as specified above.
selectTemplate
FunctionYou can provide a selectTemplate
function to dynamically determine the htmlFile
and tokenHandlers
at run time.
The function signature is:
{
selectTemplate: (request, routeOptions) => {};
}
It can return an object directly or with a Promise.
{
htmlFile: "",
tokenHandlers: [],
options: {},
cacheId: "",
cacheKey: "" // mutually exclusive with cacheId
}
htmlFile
- Path to HTML template. Its full path from path.resolve
could also be used as cacheKey
for the template instance.tokenHandlers
- Array of file paths to JS modules that implement token handlers.cacheId
- If non-empty string, then it's appended to htmlFile
to use as cacheKey
for the template instance.cacheKey
- Provide cacheKey
for the template instance, overrides cacheId
.options
- Specify some options for this route with this template:
pageTitle
- change page titleresponseForBadStatus
- callback to generate response if non-200 HTTP status.responseForError
- callback to generate response in case of error.You can access the template instance cache through
routeOptions._templateCache
If you don't want any Server Side Rendering at all, you can set the option serverSideRendering
to false
, and you can skip setting content
. This will make the server to not even load your React app.
For example:
const config = {
plugins: {
"electrode-react-webapp": {
options: {
pageTitle: "My Awesome React WebApp",
paths: {
"/{args*}": {}
},
unbundledJS: {
enterHead: [{ src: "http://cdn.com/js/lib.js" }]
},
serverSideRendering: false
}
}
}
};
To run tests
% fyn
% clap check
FAQs
Hapi plugin that provides a default React web app template
We found that electrode-react-webapp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.