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

Trouble with Redirects and Sitemap #1166

Closed
yesezra opened this issue Feb 3, 2014 · 28 comments
Closed

Trouble with Redirects and Sitemap #1166

yesezra opened this issue Feb 3, 2014 · 28 comments

Comments

@yesezra
Copy link

yesezra commented Feb 3, 2014

Hello,

I'm using the redirects feature to maintain paths when my Middleman website changes. Unfortunately, I've been experiencing some issues in the development environment.

I made a test case to illustrate my problem. In config.rb, I have a redirect set up like so:

redirect 'redirect_me/index.html', to: 'destination.html'

The redirect seems to work as expected, but when I browse to the Sitemap web view, I get the following error in the console:

NoMethodError: super: no superclass method `get_source_file' for #<Middleman::Sitemap::Extensions::Redirects::RedirectResource:0x007fb10ea7d868>

And here's what I see in my browser:
screen shot 2014-02-03 at 1 52 32 pm

Am I configuring the redirect incorrectly, or is this a bug?

Also, I haven't been able to find any documentation, other than the Cucumber feature, on the redirect feature. Is it fully supported? Can I help with documentation?

Thanks!

@tdreyno
Copy link
Member

tdreyno commented Feb 3, 2014

Thanks for the test case. I'll look into it. Seems weird that the proxy code is being called on it.

Mind trying Ruby 2.0 really quick to see if it's a 2.1 issue?

@yesezra
Copy link
Author

yesezra commented Feb 4, 2014

Thanks! Yes, I see the same behavior on 2.0:

screen shot 2014-02-03 at 5 04 04 pm

@taylor01
Copy link

I'm getting this same issue. Anyone make any progress on it?

@derekconjar
Copy link

Commenting out line #66 in proxies.rb fixed it on my end. There may be negative side effects.

https://github.com/middleman/middleman/blob/v3-stable/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb

@bhollis
Copy link
Contributor

bhollis commented Mar 2, 2014

My guess is that Middleman::Sitemap::Extensions::Redirects::RedirectResource just needs to define a get_source_file method.

@bhollis
Copy link
Contributor

bhollis commented Mar 25, 2014

Note to self, I think fixing #1217 will fix this.

@bhollis bhollis self-assigned this Mar 25, 2014
@ryancollier
Copy link

I also have this issue! Any progress?

@nemesis
Copy link

nemesis commented Oct 17, 2014

I managed to do redirect with symlinks. (Since neither this or middleman-alias worked for me.)

Say you have a file in source/path/index.md.erb and a file in source/index.md.erb and the first one to display the contents of the second one without duplicating them.

You would do the following:

$ cd source/path
$ ln -s ../index.md.erb index.md.erb

I hope this comes useful to anyone. :)

Regards.

Costea

@tdreyno
Copy link
Member

tdreyno commented Oct 17, 2014

Pretty sure this got fixed along the way, but I need to run some tests.

@nemesis
Copy link

nemesis commented Oct 17, 2014

@tdreyno I was using the last version of middleman when testing this, didn't work at all. Will the symlinks hack make things worse?

@tdreyno
Copy link
Member

tdreyno commented Oct 18, 2014

Well, symlinks won't redirect. They'll probably duplicate the output files. Does this only happen when looking at the sitemap?

@nemesis
Copy link

nemesis commented Oct 19, 2014

I haven't tried it the other way than just accessing the redirected page, which yields up an empty page.

@tdreyno
Copy link
Member

tdreyno commented Oct 20, 2014

@nemesis Can you paste the source of the empty page?

I just pushed a fix to help that error in the sitemap preview, but that wouldn't do anything to the actual functionality in your site.

@rowanhogan
Copy link

I was having this same issue, though it was when I was generating an XML sitemap (and was therefore preventing middleman build from succeeding).

I was able to solve that issue by excluding the redirect resources when printing them in the sitemap with the following method:

resources = sitemap.resources.reject { |r| r.to_param.include?("Redirects::RedirectResource") }

It's a bit of a hack, but gets the job done.

@tdreyno
Copy link
Member

tdreyno commented Apr 29, 2015

would r.is_a? Redirects::RedirectResource work? That seems reasonable to me

@rowanhogan
Copy link

Good point @tdreyno, the full class name with namespaces is Middleman::Sitemap::Extensions::Redirects::RedirectResource, so my method becomes:

resources = sitemap.resources.reject { |r| r.is_a? Middleman::Sitemap::Extensions::Redirects::RedirectResource }

@calebhailey
Copy link

Did this ever get fixed? I just ran into this today using Middleman 3.2.1 on Ruby 2.1.2.

Thanks in advance for your time and for Middleman!

@tdreyno tdreyno added the v3 label Mar 16, 2016
@milesmatthias
Copy link

Not sure if this is related or not, but I see redirects served up as plain text, not html, so the redirect doesn't happen on the client.

@tdreyno
Copy link
Member

tdreyno commented Mar 22, 2016

@milesmatthias In the Middleman preview server or on your deployed server?

@tdreyno
Copy link
Member

tdreyno commented Mar 22, 2016

It got fixed, but maybe not back ported to v3. @calebhailey is your issue the original (in the MM debug view?) or something from later in the thread?

@milesmatthias
Copy link

@tdreyno both. I'm using v3.4.0.

@tdreyno
Copy link
Member

tdreyno commented Mar 22, 2016

May not help, but give v3.4.1 a whirl?

@matiasgarciaisaia
Copy link

matiasgarciaisaia commented Aug 12, 2016

Hi there!

I've hit this very same issue while developing our website.

I'm trying to mimic our /projects/ tree with an alias /work/. So, if we have /projects/acme-inc/ and /projects/secret-stuff-x/, going to /work/acme-inc/ or /work/secret-stuff-x/ redirects you to the /projects/ pages.

The relevant code in config.rb file was like this:

ready do
  project_pages = sitemap.resources.select { |resource| resource.destination_path.start_with? 'projects/' }
  project_pages.each do |page|
    destination_path = page.destination_path
    redirect (destination_path.sub "projects/", "work/"), to: destination_path
  end
end

Like... filter all the /projects/ URLs, then make a redirect from it's /work/ counterpart to it.

As RedirectResource wasn't defining a get_source_file method, I've reopened the class to define it as the target's source_file, and, instead of passing the path in redirect's to: value, I now pass the actual middleman resource:

class Middleman::Sitemap::Extensions::Redirects::RedirectResource
  def get_source_file
    @request_path.source_file
  end
end

ready do
  project_pages = sitemap.resources.select { |resource| resource.destination_path.start_with? 'projects/' }
  project_pages.each do |page|
    destination_path = page.destination_path
    redirect (destination_path.sub "projects/", "work/"), to: page # <-- page is a Middleman::Sitemap::Resource
  end
end

If you have a path and want to get it's Resource, you can query the sitemap with sitemap.find_resource_by_path or sitemap.find_resource_by_destination_path.

Hope this helps avoid some headaches!

@sandstrom
Copy link
Contributor

I'm doing some issue-gardening 🌿🌷🌾 and came across this issue.

@matiasgarciaisaia is this an issue with Middleman? If so, would you be open to making a PR with a fix + test? That would be an awesome thing! 😎

matiasgarciaisaia added a commit to matiasgarciaisaia/middleman that referenced this issue Nov 15, 2016
So Middleman won't hang while rendering the sitemap in development
Fixes middleman#1166
@matiasgarciaisaia
Copy link

@sandstrom I wanted to test the changes, but really couldn't find how to. I've opened #2006 with the change, at least.

@sandstrom
Copy link
Contributor

@matiasgarciaisaia Awesome! ⛵️ I've commented in that issue.

@matiasgarciaisaia
Copy link

For anyone that comes by: this issue should be fixed starting in v3.3.7. Read #2006's comments for more info.

I'm not sure why @milesmatthias has seen this in v3.4.0 - are you sure about that?

I'd totally close the issue, otherwise.

@bhollis bhollis removed their assignment Mar 10, 2017
@stale
Copy link

stale bot commented Jul 17, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 17, 2021
@stale stale bot closed this as completed Jul 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests