Skip to content
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

How to compile and load a file at the same time? #809

Open
tobiasBora opened this issue Nov 10, 2020 · 3 comments
Open

How to compile and load a file at the same time? #809

tobiasBora opened this issue Nov 10, 2020 · 3 comments

Comments

@tobiasBora
Copy link
Contributor

tobiasBora commented Nov 10, 2020

Hello,

I'm implementing a function partialWithContextJSONFile that takes two parameters: $partialWithContextJSONFile("mytemplate.html", "myenv.json")$, where myenv.json is a file containing a json file that will be used as a context. So to load that file in my functionField definition, I use:

jsonBody <- B.fromString <$> loadBody (fromFilePath pathJson)
let eitherJson = Aeson.eitherDecodeStrict' jsonBody

but for this to work, I need to do:

match "menu.json" $ do
        compile getResourceBody

in the place where I define all the rules, otherwise I get an error:

Hakyll.Core.Compiler.Require.load: menu.json (snapshot _final) was not found in the cache, the cache might be corrupted or the item you are referring to might not exist

The thing is that I'd prefer to do the compilation directly in the functionField part. Any idea how I could do that? For now, here is the first part of my Context function:

partialWithContextJSONFile :: Context String
partialWithContextJSONFile = functionField "partialWithContextJSONFile" $ \args page -> do
  case args of
    [pathTemplate, pathJson] -> do
      template <- loadBody (fromFilePath pathTemplate)
      jsonBody <- B.fromString <$> loadBody (fromFilePath pathJson)
      let eitherJson = Aeson.eitherDecodeStrict' jsonBody
      [...]

Thanks!

-- EDIT --
It may be related to #802 ?

@Minoru
Copy link
Collaborator

Minoru commented Nov 10, 2020

loadBody is for Items. How about Prelude.readFile, or an alternative from bytestring or text packages?

@tobiasBora
Copy link
Contributor Author

tobiasBora commented Nov 10, 2020

Thanks for your answer. I used a similar solutions before (I was using directly the Aeson function to load files + unsafeCompiler to perform the IO), but my issue is that it seems that the compiler does not notify the changes on the .json file (need to restart the server and clear the cache), so I guess that I need somehow to say "oh, if that files changes, then recompile me".

@Minoru
Copy link
Collaborator

Minoru commented Nov 11, 2020

I guess that I need somehow to say "oh, if that files changes, then recompile me".

As far as I understand, match does precisely that. Without it, Hakyll has no idea about the file, and doesn't track its state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants