Serve static files for different route

14 views
Skip to first unread message

Conee

unread,
Feb 1, 2021, 5:43:50 PM2/1/21
to Yesod Web Framework
Regards,
I want to group my routes that are tied to an entity, but when I change their routes,
the files from the static folder cannot be found.

My routes file:

/static StaticR Static appStatic
/auth AuthR Auth getAuth

/favicon.ico FaviconR GET
/robots.txt RobotsR GET

/ HomeR GET POST
/comments CommentR POST
/profile ProfileR GET

-- routes for Manifestation I want to group with '/man/' part
/man/manHome ManHomeR GET
/man/manUser ManUserR GET
/man/manDetail ManR GET

And my error(for all images):

GET /man/static/imgs/concert1.jpeg
  Accept: image/webp,*/*
  Status: 404 Not Found 0.003943622s

When the routes were without a previous part, everything was displayed properly.
I don't know what exactly I need to change, I guess something is static-dir, but I'm not sure.
If anyone can help, I would be grateful! Thanks.

jsch...@gmail.com

unread,
Feb 2, 2021, 4:17:55 AM2/2/21
to Yesod Web Framework
Hi,

If I understand you correctly, you wonder why there is a 404 for resources below /man/static/?

In your routes file you have a route /static and you have /man/manXYZ.
But you don't have /man/static/imgs/... defined.

If you want the contents of the static folder be found under /man/static/, then you have to change the first route in your file.

Regards, Jakob

Nemanja M

unread,
Feb 2, 2021, 6:19:12 AM2/2/21
to yeso...@googlegroups.com
Hi,
Thank you for answering.

Yes you understand my problem.
And this what you mentioned is working but what if I have more then just /man/?

For example if I have :
/man/man1
/man/man2

/profile/profile1
/profile/profile2

et cetera.

Do i have then to create :

/man/static StaticR Static appStatic
/profile/static StaticR Static appStatic
?

--
You received this message because you are subscribed to a topic in the Google Groups "Yesod Web Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/yesodweb/NZVLSbF0P8M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to yesodweb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/yesodweb/67687cbe-df18-4292-8524-ead7e8ccd8edn%40googlegroups.com.


--

jsch...@gmail.com

unread,
Feb 3, 2021, 3:43:28 AM2/3/21
to Yesod Web Framework
Conee schrieb am Dienstag, 2. Februar 2021 um 12:19:12 UTC+1:

Do i have then to create :

/man/static StaticR Static appStatic
/profile/static StaticR Static appStatic
?
Yes, that would be one way. But I think it's somewhat odd to make the contents of the static folder available under each "section" (/man, /profile).

Another way would be, to create subfolders in the static folder, then you don't need to add extra routes and you can link to /static/man/my/file.

Why do you care the exact path to the resource folder? Do you link to resources with hard-coded URLs? If you really want to do that (existance of files is not checked on compile-time), than you can just use absolute URLs like "/static/man/my/file" instead of a relative URL "static/my/file".

You can also look at the definition of appStatic (it's in your app's template source) and at https://hackage.haskell.org/package/yesod-static-1.6.1.0/docs/Yesod-Static.html for special cases with more static folders.

Conee

unread,
Feb 4, 2021, 6:57:21 AM2/4/21
to Yesod Web Framework
Thanks for the reply everything is useful you wrote.

When I make 2 static routes then there is a new problem:
Multiple declarations of ‘StaticR’.

I just want my static files, for example pictures, to be seen in different sections of the routes.
For example /manHome ManHomeR, see images from static folder,
but  /man/manHome ManHomeR doesn't see the pictures:

GET /man/static/imgs/concert1.jpeg
  Accept: image/webp,*/*
  Status: 404 Not Found 0.003101984s

GET /man/static/imgs/stadium1.jpeg
  Accept: image/webp,*/*
  Status: 404 Not Found 0.004376419s

Nemanja M

unread,
Feb 4, 2021, 7:18:55 AM2/4/21
to yeso...@googlegroups.com
The problem is solved,
it seems that I did not send a good path in the hamlet file or I did not rebuild in some case.
Now works with normal path /static.
In any case, thank you very much.

--
You received this message because you are subscribed to a topic in the Google Groups "Yesod Web Framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/yesodweb/NZVLSbF0P8M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to yesodweb+u...@googlegroups.com.

jsch...@gmail.com

unread,
Feb 5, 2021, 4:49:48 PM2/5/21
to Yesod Web Framework
Your welcome :)
AFAIK, stack will only rebuild the Yesod app if haskell modules are changed but not when the template files are changed. You have to touch the module files when you change the template files. (I should write a hook in emacs for this for my setup.)
BTW, you know about using GHCi and loading DevelMain.hs which is incredible fast against stack build...?
Reply all
Reply to author
Forward
0 new messages