Skip to content

Commit 537133a

Browse files
Enhancement/issue 128 title from data (#300)
* Rfc/issue 115 build time data access (#269) * graphql server working * apollo client connect to apollo server * connected header example using lit apollo * todo * todos * query and client + server refactor * schema refactoring * clean up console logging * alias all @greenwood/cli/data module imports * avoid paramater destructuring * graphql example in the header * multiple schemas * internal data sources documentation * shelf refactor and children query integration * refactor out ApolloQuery * ability to intercept client.query calls * basic semi-working implementation * remove extra config from server context * have puppeteer wait for graphql requests before returning content * fix and add test cases for apollo * merged resolvers not actually working * multiple queries support * everything working * todos * TODO tracking * fix fallback apollo client fetch handling * full test suite * cache json test cases * stablize test due to inconsistent data results ordering * clean up deps * todo cleanup * remove forced client call in SSG mode for client * represent graph through the schema * updated data docs * typos and grammer * typos and community link fixes * hello and graph queries working together * config def and resolvers * flesh out config schema and query * remove theme file from config * delete hello query example * formatting * documentation * app template with config title from data * page title with project title * tests for custom page title * cleanup console log * docs
1 parent 7b581a2 commit 537133a

File tree

13 files changed

+100
-51
lines changed

13 files changed

+100
-51
lines changed

packages/cli/src/data/schema/graph.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ const getPagesFromGraph = async (root, query, context) => {
2222

2323
graph
2424
.forEach((page) => {
25-
const { route, mdFile, fileName, template } = page;
26-
const id = page.label;
25+
const { route, mdFile, fileName, template, title } = page;
2726
const { label } = getDeriveMetaFromRoute(route);
27+
const id = page.label;
2828

2929
pages.push({
3030
id,
3131
filePath: mdFile,
3232
fileName,
3333
template,
34-
title: label,
34+
title: title !== '' ? title : label,
3535
link: route
3636
});
3737
});
@@ -69,19 +69,19 @@ const getChildrenFromParentRoute = async (root, query, context) => {
6969

7070
graph
7171
.forEach((page) => {
72-
const { route, mdFile, fileName, template } = page;
72+
const { route, mdFile, fileName, template, title } = page;
73+
const { label } = getDeriveMetaFromRoute(route);
7374
const root = route.split('/')[1];
7475

7576
if (root.indexOf(parent) >= 0) {
76-
const { label } = getDeriveMetaFromRoute(route);
7777
const id = page.label;
7878

7979
pages.push({
8080
id,
8181
filePath: mdFile,
8282
fileName,
8383
template,
84-
title: label,
84+
title: title !== '' ? title : label,
8585
link: route
8686
});
8787
}

packages/cli/src/lifecycles/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let defaultConfig = {
99
host: 'localhost'
1010
},
1111
publicPath: '/',
12-
title: 'Greenwood App',
12+
title: 'My App',
1313
meta: [],
1414
plugins: [],
1515
themeFile: 'theme.css'

packages/cli/src/lifecycles/graph.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const createGraphFromPages = async (pagesDir, config) => {
7272
label = label || generateLabelHash(filePath);
7373

7474
// set <title></title> element text, override with markdown title
75-
title = title || config.title;
75+
title = title || '';
7676

7777
/*
7878
* Variable Definitions

packages/cli/src/plugins/meta.js

-8
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ class meta extends LitElement {
5656
header.appendChild(meta);
5757
}
5858
});
59-
60-
// handle <title> tag
61-
let title = document.createElement('title');
62-
63-
title.innerText = this.attributes.title;
64-
const oldTitle = document.head.querySelector('title');
65-
66-
header.replaceChild(title, oldTitle);
6759
}
6860
}
6961
}

packages/cli/src/templates/app-template.js

+34
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { connectRouter } from 'lit-redux-router';
33
import { applyMiddleware, createStore, compose as origCompose, combineReducers } from 'redux';
44
import { lazyReducerEnhancer } from 'pwa-helpers/lazy-reducer-enhancer.js';
55
import thunk from 'redux-thunk';
6+
import client from '@greenwood/cli/data/client';
7+
import ConfigQuery from '@greenwood/cli/data/queries/config';
8+
import GraphQuery from '@greenwood/cli/data/queries/graph';
69

710
// eslint-disable-next-line no-underscore-dangle
811
const compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || origCompose;
@@ -17,6 +20,37 @@ import './list';
1720
connectRouter(store);
1821

1922
class AppComponent extends LitElement {
23+
24+
async connectedCallback() {
25+
super.connectedCallback();
26+
const route = window.location.pathname;
27+
const response = await Promise.all([
28+
await client.query({
29+
query: ConfigQuery
30+
}),
31+
await client.query({
32+
query: GraphQuery
33+
})
34+
]);
35+
const { config } = response[0].data;
36+
const currentPage = response[1].data.graph.filter((page) => {
37+
return route === page.link;
38+
})[0];
39+
const currentPageTitleSuffix = currentPage.link === '/'
40+
? ''
41+
: ` - ${currentPage.title}`;
42+
const fullTitle = `${config.title}${currentPageTitleSuffix}`;
43+
44+
this.setDocoumentTitle(fullTitle);
45+
}
46+
47+
setDocoumentTitle(title) {
48+
const head = document.head;
49+
const titleElement = head.getElementsByTagName('title')[0];
50+
51+
titleElement.innerHTML = title;
52+
}
53+
2054
render() {
2155
return html`
2256
MYROUTES

packages/cli/src/templates/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en" prefix="og:http://ogp.me/ns#">
33
<head>
4-
<title>My App</title>
4+
<title></title>
55

66
<meta charset='utf-8'>
77
<meta name='viewport' content='width=device-width, initial-scale=1'/>
@@ -20,6 +20,7 @@
2020
<body>
2121

2222
<eve-app></eve-app>
23+
2324
</body>
2425

2526
</html>

packages/cli/test/cases/build.config.title/build.config.title.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const expect = require('chai').expect;
2727
const runSmokeTest = require('../../../../../test/smoke-test');
2828
const TestBed = require('../../../../../test/test-bed');
2929

30+
const configTitle = require('./greenwood.config').title;
3031
const mainBundleScriptRegex = /index.*.bundle\.js/;
3132

3233
describe('Build Greenwood With: ', function() {
@@ -45,7 +46,6 @@ describe('Build Greenwood With: ', function() {
4546
runSmokeTest(['public', 'not-found', 'hello'], LABEL);
4647

4748
describe('Custom Title', function() {
48-
const indexPageTitle = 'My Custom Greenwood App';
4949
const indexPageHeading = 'Greenwood';
5050
const indexPageBody = 'This is the home page built by Greenwood. Make your own pages in src/pages/index.js!';
5151
let dom;
@@ -61,7 +61,7 @@ describe('Build Greenwood With: ', function() {
6161
it('should have our custom config meta <title> tag in the <head>', function() {
6262
const title = dom.window.document.querySelector('head title').textContent;
6363

64-
expect(title).to.be.equal(indexPageTitle);
64+
expect(title).to.be.equal(configTitle);
6565
});
6666

6767
// rest of index smoke-test because <title></title> is changed for this case
@@ -116,7 +116,7 @@ describe('Build Greenwood With: ', function() {
116116
it('should have a overridden meta <title> tag in the <head> using markdown front-matter', function() {
117117
const title = dom.window.document.querySelector('head title').textContent;
118118

119-
expect(title).to.be.equal(helloPageTitle);
119+
expect(title).to.be.equal(`${configTitle} - ${helloPageTitle}`);
120120
});
121121
});
122122
});

packages/cli/test/cases/build.default.workspace-template-app/build.default.workspace-template-app.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ describe('Build Greenwood With: ', function() {
5151
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html'));
5252
});
5353

54-
it('should have a <title> tag in the <head>', function() {
54+
it('should have the default config title in the <title> tag in the <head>', function() {
5555
const title = dom.window.document.querySelector('head title').textContent;
5656

57-
expect(title).to.be.equal('Greenwood App');
57+
expect(title).to.be.equal('My App');
5858
});
5959

6060
it('should have one <script> tag in the <body> for the main bundle', function() {

packages/cli/test/cases/build.default.workspace-template-app/src/templates/app-template.js

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { connectRouter } from 'lit-redux-router';
33
import { applyMiddleware, createStore, compose as origCompose, combineReducers } from 'redux';
44
import { lazyReducerEnhancer } from 'pwa-helpers/lazy-reducer-enhancer.js';
55
import thunk from 'redux-thunk';
6+
import client from '@greenwood/cli/data/client';
7+
import ConfigQuery from '@greenwood/cli/data/queries/config';
68

79
// eslint-disable-next-line no-underscore-dangle
810
const compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || origCompose;
@@ -18,6 +20,25 @@ import './list';
1820
connectRouter(store);
1921

2022
class AppComponent extends LitElement {
23+
24+
async connectedCallback() {
25+
super.connectedCallback();
26+
27+
const response = await client.query({
28+
query: ConfigQuery
29+
});
30+
const { config } = response.data;
31+
32+
this.setDocoumentTitle(config.title);
33+
}
34+
35+
setDocoumentTitle(title) {
36+
const head = document.head;
37+
const titleElement = head.getElementsByTagName('title')[0];
38+
39+
titleElement.innerHTML = title;
40+
}
41+
2142
render() {
2243
return html`
2344
MYROUTES

0 commit comments

Comments
 (0)