-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/configinlink #1146
Feat/configinlink #1146
Conversation
Deploy preview for cms-demo ready! Built with commit 6348f0c |
Deploy preview for netlify-cms-www ready! Built with commit 6348f0c |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this!
So the use case being covered is somewhat advanced, and also very simple. Therefore, I'm thinking:
- No need to provide error messaging.
- No need to export just for testing purposes.
- We can slim this whole thing down to something like:
configUrl = _.get(document.querySelector('link[rel="netlify-cms-config"]'), 'href') || 'config.yml'
Expanding the query a bit to ensure the type is set is fine too, but that's about it.
|
@tech4him1 working on checking the type as we speak. |
src/actions/config.js
Outdated
@@ -7,6 +7,12 @@ export const CONFIG_REQUEST = "CONFIG_REQUEST"; | |||
export const CONFIG_SUCCESS = "CONFIG_SUCCESS"; | |||
export const CONFIG_FAILURE = "CONFIG_FAILURE"; | |||
|
|||
const configUrl = | |||
get( | |||
get(document.querySelectorAll('head link[rel="cms-config-url"][type="text/yaml"], [type="application/x-yaml"]')[0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe head link[rel="cms-config-url"][type="text/yaml"], [type="application/x-yaml"]
is valid, since you are checking for either head link[rel="cms-config-url"][type="text/yaml"]
or [type="application/x-yaml"]
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two thoughts:
- Just use
querySelector
and take the first one. I don't think we need robust handling here - it's an advanced use case, they should just make sure they don't add more than one. - It's cool to check the
type
property of the retrieved element directly, a bit cleaner too.
I think we should check the format separately from the selector, so that it is easier to add support for multiple formats later. Thoughts? |
Am I being too robust to ask we include support for json here? @Benaiah and I were discussing this the other day and want to make sure he has reviewed this PR also. 😄 |
src/actions/config.js
Outdated
import { authenticateUser } from "Actions/auth"; | ||
import * as publishModes from "Constants/publishModes"; | ||
|
||
export const CONFIG_REQUEST = "CONFIG_REQUEST"; | ||
export const CONFIG_SUCCESS = "CONFIG_SUCCESS"; | ||
export const CONFIG_FAILURE = "CONFIG_FAILURE"; | ||
|
||
const validTypes = [ "text/yaml", "application/x-yaml" ]; | ||
const validTypes = { TEXT_YAML: "text/yaml", APPLICATION_X_YAML: "application/x-yaml" }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use an array of types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tech4him1 I was looking through other files and saw types as an object in ListControl.js
, so I figured I'd follow in previous footsteps. I can revert back to an array, if you'd like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me think a little about how this would best work with our formats.js
for supporting multiple config formats in the future. (if you have any suggestions, that's great as well)
@brianlmacdonald Sorry for the delay, I'm ready to get this merged! I'm thinking the easiest way we would have to send this into our formats.js, is to have the accepted MIME types as the key and the formatter "name" as the value. The keys/values could be switched too, if that is easier. Example: {
"text/yaml": "yaml",
"application/x-yaml": "yaml",
"application/toml": "toml",
} |
@tech4him1 sounds good, I made the adjustments. |
src/actions/config.js
Outdated
@@ -9,6 +9,11 @@ export const CONFIG_SUCCESS = "CONFIG_SUCCESS"; | |||
export const CONFIG_FAILURE = "CONFIG_FAILURE"; | |||
export const CONFIG_MERGE = "CONFIG_MERGE"; | |||
|
|||
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml', 'application/toml': 'toml' }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless we add toml parsing for the config, we can only handle json or yaml.
src/actions/config.js
Outdated
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml', 'application/toml': 'toml' }; | ||
const configLink = document.querySelector('link[rel="cms-config-url"]'); | ||
const isValidType = link => link && validTypes[link.type]; | ||
const configUrl = isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to group all of the logic into a getConfigUrl
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You got it!
src/actions/__tests__/config.spec.js
Outdated
|
||
describe('getConfig', () => { | ||
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml', 'application/toml': 'toml' }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like TOML got left in the test accidently.
We may need to add docs at some point as well, but that can probably wait until we get more real-world use cases. |
@tech4him1 I can help with docs as soon as I finish battling the field vs fields bug. |
src/actions/config.js
Outdated
@@ -107,8 +114,9 @@ export function loadConfig() { | |||
dispatch(configLoading()); | |||
|
|||
try { | |||
const configUrl = getConfig(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you don't mind, I think it would be good to add something like:
console.log(`Netlify CMS using config file: "${ configURL }"`);
That way if a config file isn't being recognized and is defaulting to config.yml
, at least the dev knows that is happening.
Awesome, let's get it merged! Really appreciate all your work on this! |
@talves For now, our YAML parser works with JSON 😄: <link rel="cms-config-url" type="text/yaml" href="config.json"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple more things here, one may cause the CMS to crash. We should also add some docs to this before merging.
src/actions/config.js
Outdated
const getConfig = () => { | ||
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml' }; | ||
const configLink = document.querySelector('link[rel="cms-config-url"]'); | ||
const isValidType = link => link && validTypes[link.type]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we're going to crash if there's no matching link element by attempting to access property "type" of null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erquhart doesn't link && validTypes[link.type]
short circuit to false when link
is null
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♂️🤦♂️🤦♂️
Disregard!
example/index.html
Outdated
@@ -7,6 +7,9 @@ | |||
|
|||
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500' rel='stylesheet' type='text/css'> | |||
<link rel="stylesheet" href="/cms.css"/> | |||
<link href="config.yml" type="text/yaml" rel="cms-config-url"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this from the example project, only because it makes an optional feature seem necessary, and a lot of people treat this example as documentation. We could, however add a comment like:
<!--
Netlify CMS will automatically look for a "config.yml" file in the same directory
as this "index.html", but you can override this by providing a link tag like this
one:
<link href="path/to/config.yml" type="text/yaml" rel="cms-config-url">
-->
@brianlmacdonald Would you mind rebasing onto master? Just makes the diff a bit nicer to review 😄. |
@tech4him1 -- I know. It looks horrible. Lesson learned. |
@talves if you have a sec can you test this against one of your local cases? Just want to be sure before merging. |
5194660
to
681f98d
Compare
@talves Did you get a chance to test this yet? |
@tech4him1 not yet, sorry. I will try to see if I can get to it tomorrow, if you need it soon. |
@talves no rush on my part, just wanted to be sure you knew about it 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to fix the default failure on a bad link path at a minimum. This is a great contribution btw!
src/actions/config.js
Outdated
@@ -130,7 +137,9 @@ export function loadConfig() { | |||
|
|||
try { | |||
const preloadedConfig = getState().config; | |||
const loadedConfig = await getConfig('config.yml', preloadedConfig && preloadedConfig.size > 1); | |||
const configUrl = getConfigUrl(); | |||
console.log(`Netlify CMS using config file: "${configUrl}"`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should clarify this a little further. Also, maybe only show it if the link exists since the default behavior is already known. So, this console.log would move into the getConfigUrl function. That way when debugging we know the link was used rather than default. Not detrimental though, just a thought.
Using config file path: "${configUrl}"
Probably don't need to specify Netilify CMS since we already print a version on page load showing Netlify CMS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@talves Awesome! Made the changes!
src/actions/config.js
Outdated
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml' }; | ||
const configLink = document.querySelector('link[rel="cms-config-url"]'); | ||
const isValidType = link => link && validTypes[link.type]; | ||
return isValidType(configLink) ? get(configLink, 'href') : 'config.yml'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns an empty path for the default if the user supplies a link without an href. It really should return 'config.yml' on any invalid form of the link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proposed:
const getConfigUrl = () => {
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml' };
const configLinkEl = document.querySelector('link[rel="cms-config-url"]');
const isValidLink = configLinkEl && validTypes[configLinkEl.type] && get(configLinkEl, 'href');
if (isValidLink) {
console.log(`Using config file path: "${configUrl}"`);
return get(configLinkEl, 'href');
}
return 'config.yml';
}
src/actions/config.js
Outdated
const configLinkEl = document.querySelector('link[rel="cms-config-url"]'); | ||
const isValidLink = configLinkEl && validTypes[configLinkEl.type] && get(configLinkEl, 'href'); | ||
if (isValidLink) { | ||
console.log(`Using config file path: "${configUrl}"`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry Brian, missed the configUrl value here. I will let you decide how to manage it 😄
With it fixed, things look good after that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@talves done and done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All Looks good. Testing well in my use cases also!
Going to squash and merge this as soon as the Travis check completes. @brianlmacdonald thanks for the PR! |
* fix: allows for valid config types expansion * feat: config url can now come through link tag * fix: lints added coded * fix: slims down code per review * fix: expands query to find supported type * fix: removes typo in test copy * fix: changes validTypes to object * fix: groups config functions into one getConfig func * adds console message for config url * adds to docs * update docs * fix test * fix merge conflicts contributor addition moved to decaporg#1241 * avoids empty path with link without href. changes link console message * removes additional console * fixes link path in console * fix: remove superfluous .allcontributorsrc change
- Summary
Addressed issue #1132. The cms configuration file's URL had been hardcoded before. This PR allows the user to provide a
<link>
in<head>
to direct to a config file elsewhere. We grab the link out of head using a query looking forrel="cms-config-url"
, then check to make sure the type is valid and supported. If the query finds a link and it's valid we grab thehref
provided, otherwise we use the defaultconfig.yml
url.- Test plan
Aside from creating tests, I ran the CMS example code with and without the
<link>
. Without the url defaults toconfig.yml
and it works. To test the<link>
url, I created a new folder and moved theconfig.yml
file there and had thehref
pointing to it. It worked. I also added<link>
s with incorrect type and it threw the expected error.- Description for the changelog
Allows setting config URL with
<link>
in<head>
.- A picture of a cute animal (not mandatory but encouraged)
Behold, a slow loris!