spa-ssi 0.0.23 → 0.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +10 -0
  2. package/package.json +1 -1
  3. package/serve.js +12 -1
package/README.md CHANGED
@@ -37,6 +37,16 @@ In the example above, if "about.html" is not found, it tries "about.mjs" from th
37
37
 
38
38
  The assumption is that such files have an export function called "render".
39
39
 
40
+ ## Support for special *.mjs references
41
+
42
+ Along the lines of the previous section, if:
43
+
44
+ 1. Any reference is made to a file with extension *.mjs, and
45
+ 2. That javascript exports a function called "render" which expects no arguments, then
46
+ 3. If render() returns a string starting with "<", it will provide mime type text/html and render the content as HTML
47
+ 4. Else if render() returns a string starting with either "[" or "{", it will provide mime type application/json and render the content as JSOn
48
+ 5. Else it will render the content as plain text.
49
+
40
50
  ## Support for SPA
41
51
 
42
52
  If a page request doesn't resolve to a file, it defaults to /index.html
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spa-ssi",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "description": "Single Page App / Server Side Include Simple File Web Server",
5
5
  "keywords": [
6
6
  "single-page-app",
package/serve.js CHANGED
@@ -46,7 +46,18 @@ class SimpleHTTPRequestHandler {
46
46
  if(test.render && typeof test.render === 'function'){
47
47
 
48
48
  const renderedContent = test.render();
49
- res.writeHead(200, { "Content-Type": 'text/html' });
49
+ const firstChar = renderedContent[0];
50
+ let mimeType = 'text/plain';
51
+ switch(firstChar){
52
+ case '<':
53
+ mimeType = 'text/html';
54
+ break;
55
+ case '[':
56
+ case '{':
57
+ mimeType = 'application/json';
58
+ break;
59
+ }
60
+ res.writeHead(200, { "Content-Type": mimeType});
50
61
  res.end(renderedContent);
51
62
  return;
52
63
  }